title: Execute cmd script with double quotes
body: <p>Im trying to execute a <code>Process</code> with multiple parameters but they have double quotes <code>"..."</code> .</p>
  
  <p>This is how I build the script:</p>
  
  <pre><code>public void capture(String from, String to, String outputFile)
  </code></pre>
  
  <p>This method will run the command, it takes the 3 parameters which are given here:</p>
  
  <p><code>capture("0", "100", "C:\\Program Files\\myProgram\\file.txt")</code></p>
  
  <p>So the full built command looks like this:</p>
  
  <pre><code>String command = "\"C:\\Program Files (x86)\\otherProg\\prog.exe\" /dothis "
      + from + " " + to + " \"" + outputFile + "\"";
  </code></pre>
  
  <p>To see it clearly, this is the visual output of the command:</p>
  
  <p><code>"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 "C:\Program Files\myProgram\file.txt"</code></p>
  
  <p>Ok, and then I execute it like this:</p>
  
  <pre><code>String[] script = {"cmd.exe", "/c", command};
  Process p = Runtime.getRuntime().exec(script);
  </code></pre>
  
  <p>And at this point nothing happens.<br>
  The command doesnt get executed, <strong>however</strong> if I take the output:</p>
  
  <p><code>"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 "C:\Program Files\myProgram\file.txt"</code></p>
  
  <p>Copy-paste it in CMD the command <strong>DOES</strong> get executed (and i get the expected output).</p>
  
  <p>I have tried building the command like <a href="https://stackoverflow.com/questions/161859/using-quotes-within-getruntime-exec">this</a> but same effect happens.<br>
  The only possible way to run that command is to do it like this:   </p>
  
  <p><code>"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 C:\Folder\myProgram\file.txt</code></p>
  
  <p>without the quotes on last parameter and of course, without <strong>spaces</strong> in the route. </p>
  
  <p><strong>What is the solution to this?</strong></p>
  
  <p><strong>Update 1:</strong><br>
  Also tried <code>script = script.replace("\n","").replace("\t","")</code> and neither works.</p>
  
  <p><strong>Update 2:</strong><br>
  Just tried building the process like this:</p>
  
  <pre><code>Process p = Runtime.getRuntime().exec(
      "\"C:\\Program Files (x86)\\otherProg\\prog.exe\" /dothis 0 100 \"C:\\Program Files\\myProgram\\file.txt\"");
  </code></pre>
  
  <p>Passing the escaped command straight to the process <strong>does</strong> work, but why doesnt it work when they are parameters and building string with them?</p>
  
  <p><strong>SOLVED THANKS TO Tim Biegeleisen below</strong><br>
  As he mentioned, there is a problem for java to make a difference between command and parameter and when to run multiple commands, to solve this do the next:</p>
  
  <pre><code>String command = "cd \"C:\\Program Files (x86)\\otherProgram\\\" &amp; program.exe /capture "+from+" "+to+" \""+outputFile+"\"";    
  </code></pre>
  
  <p><code>&amp;</code> does it work. </p>