aspose file tools
The moose likes Threads and Synchronization and the fly likes wait runtime object finish execution Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "wait runtime object finish execution" Watch "wait runtime object finish execution" New topic
Author

wait runtime object finish execution

vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
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?


I'm not available, my BF's name is WORK.
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
Hello this is what I did:

I hope this is sufficient enough for the time being Thanks for help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait runtime object finish execution
 
Similar Threads
Running a .bat/ .cmd file from Java
Setting the classpath
invoking a batch file from java code
Process p .waitFor() waits for ever please give suggestions
Signing PDF with SecurSign