The Artima Developer Community
Sponsored Link

Java Answers Forum
process.waitFor() causes program to hang!

1 reply on 1 page. Most recent reply: Aug 14, 2003 12:26 AM by Slager .

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Stephen Huey

Posts: 10
Nickname: stephen
Registered: Aug, 2003

process.waitFor() causes program to hang! Posted: Aug 13, 2003 3:38 PM
Reply to this message Reply
Advertisement
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):

focusProcess.waitFor();

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:

BufferedInputStream bis = new BufferedInputStream(focusProcess.getInputStream());

StringBuffer sb = new StringBuffer();
int c;
while((c=bis.read()) != -1) {
sb.append(c);
}
bis.close();

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!


Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: process.waitFor() causes program to hang! Posted: Aug 14, 2003 12:26 AM
Reply to this message Reply
Besides reading the standard out stream, you must also read the error stream (getErrorStream()). So you need a thread probably to read it simultaneously. Not sure if this is problem in your case, but it might be.

Flat View: This topic has 1 reply on 1 page
Topic: Client/Server writing to database Previous Topic   Next Topic Topic: null pointer exception error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use