title: Redirect stdin and stdout in Java
body: <p>I'm trying to redirect stdin and stdout of a subprocess in java, eventually i'm going to have the output go to a JTextArea or something.</p>
  
  <p>Here is my current code,</p>
  
  <pre><code>Process cmd = Runtime.getRuntime().exec("cmd.exe");
  
  cmd.getOutputStream().write("echo Hello World".getBytes());
  cmd.getOutputStream().flush();
  
  byte[] buffer = new byte[1024];
  cmd.getInputStream().read(buffer);
  String s = new String(buffer);
  
  System.out.println(s);
  </code></pre>
  
  <p>The output looks like this:</p>
  
  <pre><code>Microsoft Windows [Version 6.1.7601]
  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
  
  C:\(Current Directory)&gt;
  </code></pre>
  
  <p>I'm expecting to see the "Hello World" string outputted.
  Maybe because the parent process isn't staying alive long enough?</p>
  
  <p>I'd also like to be able to send and receive multiple commands.</p>