java.lang/11
green@cygnus.com
green@cygnus.com
Sun Mar 5 13:16:00 GMT 2000
The following reply was made to PR java.lang/11; it has been noted by GNATS.
From: green@cygnus.com
To: bryce@albatross.co.nz, java-gnats@sourceware.cygnus.com
Cc:
Subject: Re: java.lang/11
Date: 5 Mar 2000 21:01:48 -0000
Synopsis: Process inputstream/errorstream can block
State-Changed-From-To: open->feedback
State-Changed-By: green
State-Changed-When: Sun Mar 5 13:01:48 2000
State-Changed-Why:
This is normal behaviour. The comp.lang.java.programmer FAQ
says:
6. (Sect. 18) How do I execute a command from Java?
[*] Use
Runtime.getRuntime().exec( myCommandString )
where myCommandString is something like "/full/pathname/command". An
applet will need to be signed in order to allow this.
Note, there is a gotcha associated with reading output from commands.
When the runtime exec's the process, it passes to it 3 streams, for
stdin, stdout, and stderr; the out and err are buffered but the buffer
size isn't very big. When your process runs, it reads (if needed) from
in, and writes to out and err. If it doesn't write more than the
buffer-size, it can run to completion.
But if it tries to write more data to one or the other stream than the
buffer can hold, the write blocks, and your process hangs, waiting for
you to empty the buffer so it can write some more.
So after the exec call, get the streams, and read from them in a loop
until they both hit end-of-stream (don't block on either one, just read
whatever is available from each, each loop iteration). Then when the
streams have ended, call the process.waitFor() method to let it finish
dying.
I have a test program that exhibits the failure mode
with the JDK, but not with GCJ.
I think we should just close this one.
http://sourceware.cygnus.com/cgi-bin/gnatsweb.pl?cmd=view&pr=11&database=java
More information about the Java-prs
mailing list