• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait runtime object finish execution

 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys this is my code:
------------------------------------------------------------------------
public boolean runExternal(String path) {
boolean execute = false;

try {
System.out.println(path);

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(path);
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();

} catch (Exception e) {
e.printStackTrace();
}

return execute;
}
---------------------------------------------------------------------
I will input a parameter called "path" that is actually calling an external batch file. The batch file will generate a new file, and then the new file will be used for another method, A().

However, my problem is that after my runExternal() finishes execution, it will go to A(), but then the new file is generated slower than the program goes to method A(). This will cause that method A() cannot find the new file because it is not generated yet due to slow speed

So how can I write my program, so that the program will wait until the new file is generated then only it will proceed to execute method A()? Do I need to use Thread class?
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
solved the problem I just change the code by adding a line in the runExternal() method:
-------------------------------------------------------------------
public boolean runExternal(String path) {
boolean execute = false;

try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(path);
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();

p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}


return execute;
}
--------------------------------------------------------------------
but I got another problem if I call the waitFor() method.

In my case I'm trying to call a SecurSign batch file which will encrypt my PDF file using new Runtime().exec(). I face the problem where the PDF file is corrupted, but the exec() cannot terminate and the waitFor() will goes on forever. How can I stop the execution then and interrupt the waitFor() so that it will terminate?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can spin off a couple threads to read the stdout and errout streams from the process. Those readers can look for success or error messages. Some commands hang forever if you don't read their output to end, so I'd probably always read the streams. Something like:

In my brief experience, process.wait() returned before the three streams were done. You may find the three joins sufficient to know when the command is finished. Search the ranch or maybe Google for StreamGobbler. We've had some pretty good implementations here before.

Back to your hung exec program ... try using a timeout value on one or more of the join() calls. Then you'll know the process is running too long and see if Process.destroy() will kill it.

And any time you see "repeat" think "extract to a reusable method"
[ February 22, 2006: Message edited by: Stan James ]
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello this is what I did:

I hope this is sufficient enough for the time being Thanks for help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!