My question now: How can I make (new Thread(new SyncPipe(process.getErrorStream(), System.err))) die?
You are going to have to close the input stream out from under it I believe. I suspect that it is blocked in the read and no setting of a stop
variable (even if properly volatile
) will get the read thread to unblock.
You are going to need to do something like:
InputStream is = process.getInputStream();
InputStream es = process.getErrorStream();
...
is.close();
es.close();
Code would look approximately like the following. I'm not sure if your waitFor()
call is returning or not.
InputStream is = process.getInputStream();
InputStream es = process.getErrorStream();
(new Thread(new SyncPipe(es, System.err))).start();
(new Thread(new SyncPipe(is, System.out))).start();
try {
OutputStream out = process.getOutputStream();
out.write((cmd + "\r\n").getBytes());
out.flush();
out.close();
try {
process.waitFor();
} catch ( InterruptedException e ) {
e.printStackTrace();
}
} finally {
is.close();
es.close();
}
Another answer might be to use the available()
method on InputStream
so you can loop and check your stop
flag. See this answer: https://stackoverflow.com/a/1089079/179850