<p>If I start a process via Java's <a href="http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html" rel="noreferrer">ProcessBuilder</a> class, I have full access to that process's standard in, standard out, and standard error streams as Java <code>InputStreams</code> and <code>OutputStreams</code>. However, I can't find a way to seamlessly connect those streams to <code>System.in</code>, <code>System.out</code>, and <code>System.err</code>.</p>

<p>It's possible to use <code>redirectErrorStream()</code> to get a single <code>InputStream</code> that contains the subprocess's standard out and standard error, and just loop through that and send it through my standard out—but I can't find a way to do that and let the user type into the process, as he or she could if I used the C <code>system()</code> call.</p>

<p>This appears to be possible in Java SE 7 when it comes out—I'm just wondering if there's a workaround now. Bonus points if the result of <a href="http://www.opengroup.org/onlinepubs/009695399/functions/isatty.html" rel="noreferrer"><code>isatty()</code></a> in the child process carries through the redirection.</p>

[{'answer_id': 60578, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 5, 'selected': True, 'text': '<p>You will need to copy the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Process.html" rel="noreferrer">Process</a> out, err, and input streams to the System versions.  The easiest way to do that is using the <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html" rel="noreferrer">IOUtils</a> class from the Commons IO package.  The <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#copy%28java.io.InputStream,%20java.io.OutputStream%29" rel="noreferrer">copy method</a> looks to be what you need.  The copy method invocations will need to be in separate threads.  </p>\n\n<p>Here is the basic code:</p>\n\n<pre><code>// Assume you already have a processBuilder all configured and ready to go\nfinal Process process = processBuilder.start();\nnew Thread(new Runnable() {public void run() {\n  IOUtils.copy(process.getOutputStream(), System.out);\n} } ).start();\nnew Thread(new Runnable() {public void run() {\n  IOUtils.copy(process.getErrorStream(), System.err);\n} } ).start();\nnew Thread(new Runnable() {public void run() {\n  IOUtils.copy(System.in, process.getInputStream());\n} } ).start();\n</code></pre>\n'}, {'answer_id': 1570269, 'author': 'Eelco', 'author_id': 445367, 'author_profile': 'https://Stackoverflow.com/users/445367', 'pm_score': 4, 'selected': False, 'text': "<p>A variation on John's answer that compiles and doesn't require you to use Commons IO:</p>\n\n<pre><code>private static void pipeOutput(Process process) {\n    pipe(process.getErrorStream(), System.err);\n    pipe(process.getInputStream(), System.out);\n}\n\nprivate static void pipe(final InputStream src, final PrintStream dest) {\n    new Thread(new Runnable() {\n        public void run() {\n            try {\n                byte[] buffer = new byte[1024];\n                for (int n = 0; n != -1; n = src.read(buffer)) {\n                    dest.write(buffer, 0, n);\n                }\n            } catch (IOException e) { // just exit\n            }\n        }\n    }).start();\n}\n</code></pre>\n"}, {'answer_id': 6303744, 'author': 'claude', 'author_id': 792382, 'author_profile': 'https://Stackoverflow.com/users/792382', 'pm_score': 2, 'selected': False, 'text': '<p>For <code>System.in</code> use the following <code>pipein()</code> instead of <code>pipe()</code></p>\n\n<pre><code>pipein(System.in, p.getOutputStream());\n</code></pre>\n\n<p>Implementation:</p>\n\n<pre><code>private static void pipein(final InputStream src, final OutputStream dest) {\n\n    new Thread(new Runnable() {\n        public void run() {\n            try {\n               int ret = -1;\n               while ((ret = System.in.read()) != -1) {\n                  dest.write(ret);\n                  dest.flush();\n               }\n            } catch (IOException e) { // just exit\n            }\n        }\n    }).start();\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/60302', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5696/']