Runtime.getRuntime().exec() doesn't return (?)

T

Timo Nentwig

        final String cmd = "mycmd";
        final Process proc = Runtime.getRuntime().exec( cmd );

        final BufferedReader in = new BufferedReader(new
InputStreamReader( proc.getInputStream() ));
        final StringBuffer buf = new StringBuffer();
        for( String s; (s = in.readLine()) != null; )
            buf.append( s );

System.out.println("done");

        return buf.toString();


"done" is never printed and that is actually because the for loop never
terminates. in.readLine() apparently never does return null. I assume some
process termination problem or something.

Somebody having a clue on this?

Timo
 
J

John C. Bollinger

Timo said:
final String cmd = "mycmd";
final Process proc = Runtime.getRuntime().exec( cmd );

final BufferedReader in = new BufferedReader(new
InputStreamReader( proc.getInputStream() ));
final StringBuffer buf = new StringBuffer();
for( String s; (s = in.readLine()) != null; )
buf.append( s );

System.out.println("done");

return buf.toString();


"done" is never printed and that is actually because the for loop never
terminates. in.readLine() apparently never does return null. I assume some
process termination problem or something.

Somebody having a clue on this?

I'd bet that the for loop is blocking in BufferedReader.readLine(),
presumably because the external process never terminates. Things to try:
(1) proc.getOutputStrteam().close();
(2) make sure the process error output (proc.getErrorStream()) is
drained in a separate thread.

Both might be necessary.


John Bollinger
(e-mail address removed)
 
T

Timo Nentwig

John said:
I'd bet that the for loop is blocking in BufferedReader.readLine(),
presumably because the external process never terminates. Things to try:

The command does terminate when launched in console...
(1) proc.getOutputStrteam().close();

When?
 
S

Steve Horsley

Timo said:
John C. Bollinger wrote:




The command does terminate when launched in console...
That's not relevant. If it can't print its error output to a
blocked (un-read) pipe then it won't exit. Just do what he says.

After the process exits (see above). If you don't close the
streams, you have a handle leak.


Steve
 
F

Fahd Shariff

This usually works for me:

BufferedReader br = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
// read output lines from command and add to results
String str;
while ((str = br.readLine()) != null)
{
System.out.println(str);
}

// wait for command to terminate
try {
proc.waitFor();
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}

// check its exit value
if (proc.exitValue() == 0)
// close stream
br.close();

You might also want to check the error stream:

if(proc.getErrorStream().available()!=0)
 
J

John C. Bollinger

Timo said:
John C. Bollinger wrote:

As soon as you're done writing to it, which may be immediately after
starting the process. If you do need to write to it then you probably
ought to do it in a separate thread from the ones in which you read the
process' two other streams. Processes that read their standard input
often will not terminate while that stream is open.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top