title: How do I Pipe process output to a file on Windows and JDK 6u45
body: <p>I have the following windows batch file (run.bat):</p>
  
  <pre><code>@echo off
  echo hello batch file to sysout
  </code></pre>
  
  <p>And the following java code, which runs the batch files and redirects output to a file:</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      System.out.println("Current java version is: " + System.getProperty("java.version"));
  
      ProcessBuilder pb =
              new ProcessBuilder("cmd.exe", "/c",
                      "run.bat"
                       ,"&gt;&gt;", "stdout.txt","2&gt;&gt;", "stderr.txt"
                      );
      System.out.println("Command is: " + pb.command());
  
      Process proc = pb.start();
  
      InputStream in = proc.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  
      String line = null;
      while ((line = reader.readLine()) != null) {
          System.out.println(line);
      }
  
      int exitValue = proc.exitValue();
      System.out.println("Exit value: " + exitValue);
  }
  </code></pre>
  
  <p>On JDKs up to and including JDK6u43 I get the following output:</p>
  
  <pre><code>Current java version is: 1.6.0_29
  Command is: [cmd.exe, /c, run.bat, &gt;&gt;, stdout.txt, 2&gt;&gt;, stderr.txt]
  Exit value: 0
  </code></pre>
  
  <p>and the script output is written to the file.
  As of JDK 6u45 and 7, I get the following output:</p>
  
  <pre><code>Current java version is: 1.6.0_45
  Command is: [cmd.exe, /c, run.bat, &gt;&gt;, stdout.txt, 2&gt;&gt;, stderr.txt]
  hello batch file to sysout
  Exit value: 0
  </code></pre>
  
  <p>And nothing is written to the output file.</p>
  
  <p>This may or may not be related to the changes made in Runtime.exec() , described at: <a href="http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html" rel="noreferrer">http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html</a></p>
  
  <p>What is the correct way of starting a process on Windows with output redirected to files?</p>
  
  <p>Note: In a real world scenario, the command to execute may include parameters with spaces, as in:</p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c",
  "run.bat", "Some Input With Spaces", 
  "&gt;&gt;", "stdout.txt","2&gt;&gt;", "stderr.txt");
  </code></pre>