<p>When launching a process from Java, both stderr and stdout can block on output if I don't read from the pipes.  Currently I have a thread that pro-actively reads from one and the main thread blocks on the other.</p>

<p>Is there an easy way to join the two streams or otherwise cause the subprocess to continue while not losing the data in stderr? </p>

[{'answer_id': 64145, 'author': 'killdash10', 'author_id': 7621, 'author_profile': 'https://Stackoverflow.com/users/7621', 'pm_score': 0, 'selected': False, 'text': '<p>Just have two threads, one reading from stdout, one from stderr?</p>\n'}, {'answer_id': 64183, 'author': 'Mat Mannion', 'author_id': 6282, 'author_profile': 'https://Stackoverflow.com/users/6282', 'pm_score': 3, 'selected': True, 'text': "<p>Set the redirectErrorStream property on ProcessBuilder to send stderr output to stdout:</p>\n\n<pre><code>ProcessBuilder builder = new ProcessBuilder(command);\nbuilder.redirectErrorStream(true);\n</code></pre>\n\n<p>You should then create a thread to deal with the process stream, something like the following:</p>\n\n<pre><code>Process p = builder.start();\n\nInputHandler outHandler = new InputHandler(p.getInputStream());\n</code></pre>\n\n<p>Where InputHandler is defined as:</p>\n\n<pre><code>private static class InputHandler extends Thread {\n\n    private final InputStream is;\n\n    private final ByteArrayOutputStream os;\n\n    public InputHandler(InputStream input) {\n        this.is = input;\n        this.os = new ByteArrayOutputStream();\n    }\n\n    public void run() {\n        try {\n            int c;\n            while ((c = is.read()) != -1) {\n                os.write(c);\n            }\n        } catch (Throwable t) {\n            throw new IllegalStateException(t);\n        }\n    }\n\n    public String getOutput() {\n        try {\n        os.flush();\n        } catch (Throwable t) {\n            throw new IllegalStateException(t);\n        }\n        return os.toString();\n    }\n\n}\n</code></pre>\n\n<p>Alternatively, just create two InputHandlers for the InputStream and ErrorStream. Knowing that the program will block if you don't read them is 90% of the battle :)</p>\n"}]
['https://Stackoverflow.com/questions/64000', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4926/']