Follow up on the Process API in Java
Photo by Garett Mizunaka on Unsplash
In my last blog post, I focused on the improvements made in the process API and proposed an alternative implementation, for a more fine grained control on the threads executing
It appears that after digging, I realized that a process may be blocked if it does not drain its output stream. There is tons of blog posts about this subject. If you are not interested in the output and error output (
stdout), you can redirect them to
/dev/null easily with the
ProcessBuilder API (since Java 7) :
new ProcessBuilder().redirectErrorStream(true).redirectOutput(new File("/dev/null"));
This simple code will redirect the error stream into the output stream (merging
stdout) and will write to
/dev/null, which will behave as a black hole.
Then, to better handle the output and error of an external process, either redirect explicitly to a file with the
ProcessBuilder API, or create a mechanism to drain the output/error.