wood burning stoves
The moose likes Java in General and the fly likes waitfor on a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "waitfor on a process" Watch "waitfor on a process" New topic

waitfor on a process

Bryant Lee

Joined: Feb 24, 2004
Posts: 9
I'm writing a java program that calls a long and complicated c program. When I collect the input stream everything works. The following works for me:
try {
String cmd = c program name and command line arguments
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line = input.readLine()) != null) {
But, if I use p.waitFor() the c program does not finish running. In the following:
try {
Process p = Runtime.getRuntime.exec(cmd);
The C program runs about halfway (it generates 4 jpegs when it should generate 10). And "Done" is never printed.
This is very frustrating since I have tried this code by itself, in its own main function (no other threads or functions or anything) and it still does the same thing. If anyone could help me with this question I would I really appreciate it. Thanks.
-Bryant Lee
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Bryant,
Welcome to JavaRanch!
The output produced by the external program goes into a buffer of finite size. When that buffer fills up, the program gets a SIGPAUSE (on Unix; on Windows, I believe there's a similar mechanism.) Anyway, the external program doesn't run anymore until there's room in the buffer for more output.
So you need to either drain the output buffer (as you are doing in the first version of your program) or run the program as part of a shell script that sends output to /dev/null . Note that if you're draining it, you don't need to display it -- you can simply discard it. You don't need to use BufferedReader, either, which adds overhead; a simple

will do. You can spawn this off into its own thread if you want.

[Jess in Action][AskingGoodQuestions]
Bryant Lee

Joined: Feb 24, 2004
Posts: 9
Thank you, now that I'm draining the buffer it works beautifully.
btw, I noticed you're down at Sandia. Right now I'm doing an internship at ARL (old Harry Diamond) which is what I was writing that program for. Just thought that was interesting. Anyway, thank you again.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've done something like this with two threads to read stdout and errout. I noticed that the threads ran long after waitfor() returned. So I quit using waitfor() and waited for both of those threads to read to end. Then I know I'm done and can return from the primary method. Whew.

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
I agree. Here's the link: http://aspose.com/file-tools
subject: waitfor on a process
It's not a secret anymore!