Java exec() external process

Status
Not open for further replies.

Mark Cianciosa

Wise, Aged Ars Veteran
161
Can some one explain to me why the following code is causing my external process to sleep and never exit
Code:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(someCommand, null, somedirectory);
p.waitFor();
but the code
Code:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(someCommand, null, somedirectory);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
br.close();
p.waitFor();
makes my external process execute cleanly and exit? I'm running this on Solaris. In the first code example the process I'm trying to run executes but instead of exiting afterwards the process goes to sleep so my java program hang on p.waitFor().
 

bigmac44

Wise, Aged Ars Veteran
103
The external process is trying to write something to stdout that you aren't consuming in the first example. At some point its output buffers fill up and then blocks while trying to write (e.g. in a call to printf()), waiting for you (the parent java process) to free space in the buffer by consuming what it has written. By calling close() on the buffered reader, I would guess that you are closing the stdout stream of the external process. This causes all writes to stdout to fail (and the process most likely isn't checking to see if its writes are actually successful), but prevents any blocking.
 
Status
Not open for further replies.