title: How to start a command in windows under java that is completely detached
body: <p>I want to start a processes such that the JVM can die but the spawned processes continues to run even if it is writing to STDOUT.</p>
  
  <p>I first tried using a <code>ProcessBuilder</code> with the output set to Files and passing in:</p>
  
  <pre><code>cmd /c myCmd.exe arg0 arg1
  </code></pre>
  
  <p>However even after closing all Input/Output streams, if I call <code>Process#.waitFor</code>, it does not return until <code>myCmd.exe</code> has finished. It seems it is still attached to the JVM in some way (even though the JVM can probably die at this point and not affect the child proc).</p>
  
  <p>I then tried the <code>start</code> command, it seems that is not on the path (I couldn't find the bin in <code>c:\windows</code>) so I ran it under <code>cmd</code> the arguments (separated by space) passed to <code>ProcessBuilder</code> became:</p>
  
  <pre><code>cmd /c start /b myCmd.exe arg0 arg2 &gt;log 2&gt;&amp;1
  </code></pre>
  
  <p>That results in:</p>
  
  <ul>
  <li>✓ <code>Process#.waitFor</code> returning before <code>myCmd.exe</code> finished.</li>
  <li>⚠ It seemed that I needed to use a different log file from the one passed to the <code>ProcessBuilder</code></li>
  <li>✘ I then found the escaping become weird if the command run was <code>echo</code> and the argument was <code>^^^^\foo</code> it would write to the log file <code>^\foo</code>, I also noticed if I gave it <code>"^^^^\foo"</code> it would return the same thing ie <code>"^^^^\foo"</code>.</li>
  </ul>
  
  <p>So:</p>
  
  <ol>
  <li>Is calling <code>cmd.exe /c start /b</code> the correct thing to do?</li>
  <li>Am I doing something wrong with the escaping (which is really what I give to process builder), should I perhaps be doing something different because of <code>cmd.exe</code> calling <code>start</code>, perhaps I need to actually escape in some way? Perhaps I don't understand windows processes do they even have proper support for taking an array of arguments?</li>
  <li>Am I going about this the wrong way should I be trying to call a native library from <code>C</code>? If so what would it be I don't mind if I have to call a <code>C</code> program to get my process running in the background.</li>
  </ol>