title: Java process in Docker container doesn't exit on end of main()
body: <p>I have a java jar command, and I'm calling it from inside python using Popen. I'm happy that I'm doing it right, and theres lots of reasons why I'm doing it this way (its not great, but it is what it is).</p>
  
  <p>When I run the Java locally, it works fine (JRE 1.8), when I run the python locally that calls the Java, it works fine. When I run it inside a docker container, it just hangs - the python on the process.communicate() and when i do a docker exec, it just does not return (the output from the command is exactly as I expect).</p>
  
  <p>What I've discovered that unless theres an explicit <code>System.exit(0);</code> at the end of the Java, then the Java process does not end <strong>when run inside of a docker container</strong> - it just hangs, at the point of all processing being done.</p>
  
  <p>To check this, I made a Hello World app consisting of 5 lines (the System.exit i comment out or in to check that this works or doesn't)</p>
  
  <pre><code>public class Hello {
      public static void main(String[] args) {
          System.out.println("Heellllooo");
          // System.exit(0);
      }
  }
  </code></pre>
  
  <p>I bundled this into a Jar and put it in the folder where the 'real' jar sits, and execute.</p>
  
  <p><strong>Without the <code>system.exit()</code> the process hangs. With it it exits cleanly.</strong> In both instances, 'Heelllloooo' is printed.</p>
  
  <p>Other random facts that might help diagnosis the command i'm using to check is this: (the popen in python is much more complex with the arguments I'm passing into Java).</p>
  
  <pre><code>docker exec [tag] java -jar libs/Hello.jar
  </code></pre>
  
  <p>If I do:</p>
  
  <pre><code>docker exec [tag] java -version
  </code></pre>
  
  <p>it returns immediately with the version info. </p>
  
  <p>I'm running a Mac, so this is running inside of a docker-machine boot2docker Ubuntu vm thing. Other people running the same docker-images on Debian and Ubuntu machines do not have any problems with this at all.</p>
  
  <p>I don't believe the problem is anything to do with Python because the symptoms are there when running it from the exec (hence me not tagging it python).</p>
  
  <p><strong>My question: Why doesn't the java process exit when the main function returns?</strong></p>
  
  <p><em>To be clear, I don't want to know how to kill the process (I know how to do that), what I want to know is why the process does not return. (and this seems to be a specific java / docker thing).</em></p>