BlogKontaktTagcloud

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.

int retVal = 0;
boolean finish = false;
while( !finish ){
	try{
		retVal = proc.exitValue();
		finish = true;
	} catch(IllegalThreadStateException e){
		try {
			Thread.sleep(10);
		} catch (InterruptedException e2) {}
	}
}

By the way, this is my first english blog entry. (So, sorry for the mistakes!)

Ähnliche Beiträge:
Named parameters in Java (bgl-style)
SCJP, now!
Mobile App Hackathon
Make it human (or how to crack a CAPTCHA)
Jira status
Comments (19)  Permalink

comments

Emanuel @ 12.09.2005 10:07 CEST
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.
leo @ 12.09.2005 12:24 CEST
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 13:37 CEST
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 ;)
leo @ 12.09.2005 13:56 CEST
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 22:51 CEST
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.
david @ 31.01.2006 17:15 CEST
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 !
leo @ 31.01.2006 19:27 CEST
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.
leo @ 05.03.2006 11:40 CEST
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 @ 04.04.2006 09:59 CEST
while( finish )??? Are u sure?
while( !finish )??
leo @ 06.04.2006 12:29 CEST
Hu Ming your right! There was a typing mistake (shame on me). I fixed this now.
Sridhar Yamsani @ 04.10.2007 07:05 CEST
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.
leo @ 25.10.2007 20:04 CEST
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.
Yegor @ 21.02.2008 16:45 CEST
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"
leo @ 26.02.2008 00:22 CEST
Yegor, I have no clue what you do! But acctually this bug should not happen in your version anymore.
M@ @ 18.06.2008 17:53 CEST
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 17:01 CEST
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 09:05 CEST
hi... java.nerd.BlogReader();.. thanx for ur suggestion.. it really helped me ....
d1 @ 02.10.2008 15:57 CEST
hello java.nerd.BlogReader();,
this was the correct solution! thanx!
Michal Rakus @ 07.10.2008 11:27 CEST
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!

Name*
E-Mail
For Spammers Only
URL
Kommentar*
E-Mail Benachrichtigung bei neuen Kommentaren zu diesem Eintrag
Speichere meine Daten (braucht Cookies)