title: How to get ProcessBuilder to handle nested quotes?
body: <p>I am having problems getting <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html" rel="nofollow">ProcessBuilder</a> to execute a command-line the same way as the cmd.exe console.</p>
  
  <ol>
  <li>The command line is:
  <code>show_parameters.bat jdk-1_5_0_22-windows-i586-p.exe /s /v"/qn INSTALLDIR=\"C:\Program Files (x86)\gili\""</code></li>
  <li>The batch file <code>show_parameters.bat</code> (below) shows the tokens that cmd.exe breaks the command-line into.</li>
  <li><code>Testcase.java</code> (below) attempts to execute the same command-line as #1 using ProcessBuilder.</li>
  <li><p>If you run <code>show_parameters.bat jdk-1_5_0_22-windows-i586-p.exe /s /v"/qn INSTALLDIR=\"C:\Program Files (x86)\gili\""</code> you will get:</p>
  
  <pre><code>Console tokens:
  
  jdk-1_5_0_22-windows-i586-p.exe
  /s
  /v"/qn INSTALLDIR=\"C:\Program
  Files
  (x86)\gili\""
  </code></pre></li>
  <li><p>If you run <code>java Testcase</code> you will get:</p>
  
  <pre><code>Java tokens: [cmd.exe, /c, show_parameters.bat, jdk-1_5_0_22-windows-i586-p.exe,
   /s, /v"/qn INSTALLDIR=\"C:\Program Files (x86)\gili\""]
  
  Console tokens:
  
  jdk-1_5_0_22-windows-i586-p.exe
  /s
  "/v"/qn
  INSTALLDIR
  \"C:\Program Files (x86)\gili\"
  ""
  </code></pre></li>
  </ol>
  
  <p><strong>Is it possible to cause ProcessBuilder to produce the same tokenization as #1?</strong> Or is this a bug in Java?</p>
  
  <hr>
  
  <h3>show_parameters.bat</h3>
  
  <pre><code>@echo off
  SETLOCAL ENABLEDELAYEDEXPANSION
  
      echo.
      echo Console tokens:
      echo.
  
      :again
      if [%1] == [] goto end
        echo %1
        shift
        goto again
      :end
  </code></pre>
  
  <h3>Testcase.java</h3>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  
  public class Testcase
  {
  
      public static void main(String[] args) throws IOException, InterruptedException
      {
          String base = "C:\\Program Files (x86)\\gili";
          ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", "show_parameters.bat", "jdk-1_5_0_22-windows-i586-p.exe", "/s",
                  "/v\"/qn INSTALLDIR=\\\"" + base + "\\\"\"");
          processBuilder.redirectErrorStream(true);
          System.out.println("Java tokens: " + processBuilder.command());
          Process process = processBuilder.start();
          BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
          while (true)
          {
              String line = in.readLine();
              if (line == null)
                  break;
              System.out.println(line);
          }
      }
  }
  </code></pre>