title: How to redirect child process stdout/stderr to the main process stdout/stderr in Java?
body: <p>In other languages (like <em>bash</em> and <em>Python</em>), when we spawn a child process, this new process will inherit the stdout and stderr from the parent. This means that any output from the child process will be printed to the terminal as well as the output from the parent.</p>
  
  <p>How can we achieve the same behavior in Java?</p>
  
  <p>My first try was:</p>
  
  <pre><code>proc = Runtime.getRuntime().exec(cmd);
  </code></pre>
  
  <p>But it won't work. Based on <a href="https://stackoverflow.com/a/3644288/124946">this answer</a> and <a href="https://stackoverflow.com/a/3641817/124946">this answer</a>, I've replaced the code with:</p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder(cmd);
  pb.redirectOutput(System.out);
  pb.redirectError(System.err);
  </code></pre>
  
  <p>But this doesn't even compile, as the arguments are incompatible with the expected method parameters.</p>