File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

waitfor on a process

 
Bryant Lee
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
System.out.println(line);
}
input.close();
}
...
But, if I use p.waitFor() the c program does not finish running. In the following:
try {
Process p = Runtime.getRuntime.exec(cmd);
p.waitFor();
}
...
System.out.println("Done");
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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bryant Lee
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic