• Post Reply Bookmark Topic Watch Topic
  • New Topic

process.waitFor() causes program to hang!  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've successfully created a Process and run it using Runtime's exec(String path), and the kinds of processes I've successfully run have included Winzip, a WS_FTP Pro script, and a regular .bat file. I've also successfully called waitFor() on these processes, but in this particular case I'm calling a program that takes a file I give it as a parameter and merges its data into a database. I've been doing this procedure for a while (either from a command line or in a .bat file), but now I need to call it from my Java code. Preferably I'd like to waitFor() the process each time because I need to make sure all the files are merged in chronological order. My current test case only uses ONE file, but in reality there will be several. With one file, I can run the program and it appears to run fine (and it runs fast--like maybe a second or two at most), but whenever I call the following line it hangs indefinitely (I've never seen it terminate, even after several minutes):

When I just execute that program by itself by calling a .bat file with a hardcoded filename, I get standard output back from the program I'm calling. I'd like to see this output when I run my Java program and it appears to run fine, because I have no way if it IS running correctly! So, I added the following:

I'm not sure if this is the right way to monitor that InputStream that I get from the process, and even if that InputStream is going to give me the standard output from that the process normally writes to the terminal. All I do know is that again, the program seems to hang indefinitely, and I guess it has to do with the fact that maybe this process isn't notifying me that it's terminated, and so I'm still waiting for the rest of that InputStream. And yes, I HAVE tried both of these situations together AND separately (so I know that both pieces of code cause the program to hang).
Any ideas would be much appreciated!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Processes oftentimes won't exit until you're read all the output they produce, on both stdout and stderr. The code you're got is a good start, but you may need to read everything from the error stream, too.
Interactive processes sometimes won't exit until their standard input it closed, so getOutputStream().close() can't hurt, either.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, you may very well need to read from error stream and standard output in separate threads. The process may be waiting for you to read the error before it can proceed, or it may be waiting for standard output. Since you don't know which it is, read both in separate threads.
Life may be easier if you use commanline options to simply redirect output to a file, e.g.:
runtime.exec("someprog.exe 1>outfile.txt 2>&1");
This should send both standard output (1) and error (2) to the same file, outfile.txt. At least, in most Unix and Windows I've tried it on (NT and 2000). That way, you don't need to worry about collecting it from the Process.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Huey:
I've successfully created a Process and run it using Runtime's exec(String path), and the kinds of processes I've successfully run have included Winzip, a WS_FTP Pro script, and a regular .bat file. I've also successfully called waitFor() on these processes, but in this particular case I'm calling a program that takes a file I give it as a parameter and merges its data into a database. I've been doing this procedure for a while (either from a command line or in a .bat file), but now I need to call it from my Java code. Preferably I'd like to waitFor() the process each time because I need to make sure all the files are merged in chronological order. My current test case only uses ONE file, but in reality there will be several. With one file, I can run the program and it appears to run fine (and it runs fast--like maybe a second or two at most), but whenever I call the following line it hangs indefinitely (I've never seen it terminate, even after several minutes):

This code pauses on my machine indefinitely when trying to use the DOS version of WinZip. I am not sure why.
Could you please tell me if you had to tweak it somehow so it would not just pause forever? The WZUNZIP.exe also sits in my process queue forever as well even though the files have been unzipped.
I am using an Oracle 9i HTTP server with JSP. Any help would be appreciated... sorry for a possible thread hijack and you are welcome to e-mail me at jgrant@stiusa.com to avoid it.
Thanks in advance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!