title: How to read the std output of another java program in this java program?
body: <p>I write a simple Java program, which just output some "hello"s to std every 5s.</p>
  
  <pre><code>public class DDD {
      public static void main(String[] args) throws InterruptedException {
          for (int i = 0; ; i++) {
              System.out.println("hello " + i);
              Thread.sleep(5000);
          }
      }
  }
  </code></pre>
  
  <p>Then I compile it and getting a .class.</p>
  
  <p>I write another java program to run it and get the output:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      String command = "c:\\java\\jdk1.7.0_07\\bin\\java mytest.DDD";
      Process exec = Runtime.getRuntime().exec(command);
  
      BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
      while (true) {
          String line = reader.readLine();
          System.out.println(line);
          if (line == null) {
              Thread.sleep(1000);
          }
      }
  }
  </code></pre>
  
  <p>But it always prints:</p>
  
  <pre><code>null
  null
  null
  null
  null
  null
  null
  </code></pre>
  
  <p>Where is wrong? My os is "windows XP".</p>