Java Bug: Process.waitFor() hangs
Yesterday I try out to execute a command on my local Windows XP machine with the Java 1.4.2_01 with the Runtime Class. So I use Runtime.getRuntime().exec("ipconfig"), thats work's great, but afterwards I try to call the methode waitFor on the returned process. Now my Application hangs.
So I search for this bug in Google and find some (perhaps) related bug in the bug database from sun. Special the bug #4750978 looks very familiar. Also when you search in the bug database you will find a lot of similar bugs, I think sun have some problems with this class.
So I have created a small, nasty workaround.
So I search for this bug in Google and find some (perhaps) related bug in the bug database from sun. Special the bug #4750978 looks very familiar. Also when you search in the bug database you will find a lot of similar bugs, I think sun have some problems with this class.
So I have created a small, nasty workaround.
int retVal = 0;By the way, this is my first english blog entry. (So, sorry for the mistakes!)
boolean finish = false;
while( !finish ){
try{
retVal = proc.exitValue();
finish = true;
} catch(IllegalThreadStateException e){
try {
Thread.sleep(10);
} catch (InterruptedException e2) {}
}
}
comments
I know I have to upgrade, will do this after my exames. But when you distributing software to other peoples you still have this problem.
Emanuel
@ 12.09.2005 12:37 CET
Your right in this case i would create an instaler and bundle a suitable JVM with the software. http://www.ej-technologies.com/products/install4j/overview.html
PS: bisch morn z rappi ;)
Your right in this case i would create an instaler and bundle a suitable JVM with the software. http://www.ej-technologies.com/products/install4j/overview.html
PS: bisch morn z rappi ;)
Yeah, and download 30 Megs for a software which will fit into a 100 KB. I'am until the first exames always in Rappi!
jingle
@ 28.10.2005 21:51 CET
I think it also depends how long the process itself takes. If it is quick (prior to hitting waitFor then there is no problem). If it takes longer and it hits the waitFor, then it will hang.
I think it also depends how long the process itself takes. If it is quick (prior to hitting waitFor then there is no problem). If it takes longer and it hits the waitFor, then it will hang.
I had this problem also with the waitForMethod(). I launched a process which was an http server, and when I try to wait for it via waitFor() the server could not respond.
I found that solution that worked for me : use the process inputStream.
and then simply read from this inputstream in a while statement.
This avoid "try" "catch" tricks and allow, which can be interested also, to display process system.out in my soft !
jingle is right, when you call waitFor() after the process is finish it won't hang.
David I'm not sure if the InputStream would work in my case cause I don't know how long I have to read from the input stream.
Search again for this Bug today and it seams like it was bug #4763362 and sun fixed it in release 1.4.2.
Hu Ming your right! There was a typing mistake (shame on me). I fixed this now.
Hi,
I also faced the same problem with waitFor().Leo you said that sun fixed in 1.4.2 but I am facing the same problem with 1.5 also. The workaround is working fine in windows xp also.
For me it worked without the workaround in Version 1.5.0_04, but there might be an other but in other versions. But I'm glad that my workaround works for you.
looks like I have such bug now!!! Do not know what to do
i use sdk 1.6.0_02 / WinXP
the program hangs on waitFor(), even more the process is alive in TaskManager forever, which is not expected for processes started as "java -version"
Yegor, I have no clue what you do! But acctually this bug should not happen in your version anymore.
M@
@ 18.06.2008 16:53 CET
I agree this is a nasty work around. But seems to be the only way sometimes... seems this bug has not been fixed as well as believed. Im using 1.6 on XP and same old story. Your workaround works great. Thanks! (what 3 years after first done)
I agree this is a nasty work around. But seems to be the only way sometimes... seems this bug has not been fixed as well as believed. Im using 1.6 on XP and same old story. Your workaround works great. Thanks! (what 3 years after first done)
java.nerd.BlogReader();
@ 06.08.2008 16:01 CET
I will try it in English....
I solved this bug by reading the Inputstream of the process _before_ calling process.waitFor().
The process hangs because the I/O Buffer is full and must be cleared by reading it ....
Hopes this helps anyone
I will try it in English....
I solved this bug by reading the Inputstream of the process _before_ calling process.waitFor().
The process hangs because the I/O Buffer is full and must be cleared by reading it ....
Hopes this helps anyone
Ashwin
@ 08.09.2008 08:05 CET
hi... java.nerd.BlogReader();.. thanx for ur suggestion.. it really helped me ....
hi... java.nerd.BlogReader();.. thanx for ur suggestion.. it really helped me ....
Michal Rakus
@ 07.10.2008 10:27 CET
Very good article about this Problem and also very good Solution:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Very good article about this Problem and also very good Solution:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
add a comment
The Trackback URL to this comment is:
http://leo.freeflux.net/blog/plugin=trackback(139).xml
This blog is gravatar enabled.
Your email adress will never be published.
Comment spam will be deleted!






Hi Leo,
I tried to reproduce this bug with Java 1.4.2_07 and 1.5.0_04. It seems that this bug is fixed in both versions.