title: Java ProcessBuilder: external process hangs
body: <p>I'm using Java's ProcessBuilder class to run an external process. The process should not terminate before the Java program does; it must stay alive in command/response mode.</p>
  
  <p>I know that the process streams may easily 'jam' if neglected, so I've done the following:
  The program reads the process's combined output and error streams in a "reader" thread, and uses a "writer" thread to manage the commands. The reader thread does blocking character reads from process output, buffers them up into Strings and dispatches the results. The writer thread writes complete "command" lines via a PrintWriter; it uses a queue to ensure that no two command writes are "too close together" (currently 100ms), and that no new command gets written before the output of the previous command is complete. I also call flush() and checkError() after every println(). </p>
  
  <p>This scheme works fine for a few seconds or minutes, then the reader thread hangs on the blocking read(). No errors, no exceptions thrown, no more process output. Thereafter nothing will revive the external process (short of restarting it). (BTW this happens on both Linux and Windows.)</p>
  
  <p>I've looked at the code and test-cases in Jakarta Commons Exec and in Plexus Utils <a href="http://plexus.codehaus.org/plexus-utils/" rel="nofollow noreferrer">http://plexus.codehaus.org/plexus-utils/</a> but (a) neither gives an example of using a long-lived Process and (b) neither appears to be doing anything basically different from what I've described.</p>
  
  <p>Does anyone have a clue what's happening here please? 
  Thanks!</p>