title: Runtime.exec().waitFor() not actually waiting for
body: <p>I've got some code that uses Runtime.exec() to run an external .jar (built as an IzPack installer). </p>
  
  <p>If I run this external.jar from the command line like so:</p>
  
  <pre><code>java -jar external.jar
  </code></pre>
  
  <p>Then the command prompt does not return control until the application is finished. However, if I run external.jar from within some java class, using:</p>
  
  <pre><code>Process p = Runtime.getRuntime().exec("java -jar external.jar");
  int exitCode = p.waitFor();
  System.out.println("Process p returned: " + exitCode);
  </code></pre>
  
  <p>Then <code>p</code> returns almost instantly with a success code of <code>0</code>, despite external.jar having not yet completed execution (i've also tried this via the <code>ProcessBuilder</code> route of external file execution).</p>
  
  <p>Why does it wait to return from the command line, but not when executed from within another java program?</p>
  
  <p>I've also set up 3 jars, A, B and C where A calls B which calls C (using <code>Runtime.exec()</code>), where C <code>Thread.sleep</code>s for 10 seconds, as a simple test, and as expected, A doesn't return until 10 seconds after it runs.</p>
  
  <p>I figure this is probably some kind of a threading issue with external.jar where execution is being handed over from one thing to another, but given that it works directly from the command line i kind of expected to see the same behaviour (perhaps naively) when called from within another java program.</p>
  
  <p>I've tested this on Windows and Ubuntu with Java 6.</p>
  
  <p>Thanks!</p>