using Process.waitFor(), but BufferedReader on output stream not ready??

I

ittay.dror

Hi,

I have the following code:
Process proc = Runtime.getRuntime().exec(cmd, env);

int rc = proc.waitFor();

BufferedReader reader = new BufferedReader(new InputStreamReader(
proc.getInputStream(), 8192);

if (reader.ready()) {
THIS CODE IS NEVER REACHED
}

.....

I don't know what to make of this. If I avoid using ready() and use
readLine() directly, the code is blocked forever.

Please help,
Thanx,
Ittay
 
N

Neil Campbell

Hi,

I have the following code:
Process proc = Runtime.getRuntime().exec(cmd, env);

int rc = proc.waitFor();

BufferedReader reader = new BufferedReader(new InputStreamReader(
proc.getInputStream(), 8192);

if (reader.ready()) {
THIS CODE IS NEVER REACHED
}

.....

I don't know what to make of this. If I avoid using ready() and use
readLine() directly, the code is blocked forever.

Hi,

What happens if you try to read from the BufferedReader before calling
proc.waitFor() ?
 
I

ittay.dror

I didn't check, but the javadoc clearly states I'd get an exception if
I try to get the exit value before the process finished, and I need to
know the exit value.
 
I

ittay.dror

btw, this happens only on several specific machines. all are using jdk
1.4.0_02 but it seems that the *faster* ones have the problem. the OS
is redhat 7.3

ittay
 
N

Neil Campbell

I didn't check, but the javadoc clearly states I'd get an exception if
I try to get the exit value before the process finished, and I need to
know the exit value.

I was thinking more along the lines of reading the data, then calling
waitFor afterwards to get the exit value. I'm not sure if this makes any
difference or not, but as it stands I would think a process that generates
a lot of output may run out of space in the output buffer and block. This
would mean that it would never complete and your call to waitFor would not
return.
 
I

ittay.dror

i would agree, but my process generates a max of 10 characters (a short
message, which it outputs by echo).

btw, i don't know if it matters, but it's a shell (bash)

Ittay
 
I

ittay.dror

thanx for the help.

the solution finally was that this script, when run from java, didn't
write anything to stdout, and returned 0. because it didn't write
anything to stdout, ready() returned false (though i would have
expected it to return true, and then read() to return EOF)

when checked from the shell, it worked fine, so that threw me off.
 

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

Forum statistics

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

Latest Threads

Top