That said, the undocumented proper way of handing a Process object and its corresponding I/O streams is to wrap the exec() call in a try-finally block, closing the
STDOUT
, STDIN
, and STDERR
streams when you’re done with the Process object. The abstract class java.lang.Process exposes these three streams to you via getOutputStream(), getInputStream() andgetErrorStream() which you must explicitly close.Process p = null;
try {
p = Runtime.getRuntime().exec(...);
// Do something with p.
} finally {
if(p != null) {
closeQuietly(p.getOutputStream());
closeQuietly(p.getInputStream());
closeQuietly(p.getErrorStream());
}
}
Read full article from Mark S. Kolich - Remember to Close Your Streams When Using Java's Runtime.getRuntime().exec()
No comments:
Post a Comment