title: Is *this* really the best way to start a second JVM from Java code?
body: <p>This is a followup to my <a href="https://stackoverflow.com/questions/1218790/">own previous question</a> and I'm kind of embarassed to ask this... But anyway: how would you start a second JVM from a standalone Java program in a system-independent way? And without relying on for instance an env variable like JAVA_HOME as that might point to a different JRE than the one that is currently running. I came up with the following code which actually works but feels just a little awkward:</p>
  
  <pre><code>public static void startSecondJVM() throws Exception {
      String separator = System.getProperty("file.separator");
      String classpath = System.getProperty("java.class.path");
      String path = System.getProperty("java.home")
                  + separator + "bin" + separator + "java";
      ProcessBuilder processBuilder = 
                  new ProcessBuilder(path, "-cp", 
                  classpath, 
                  AnotherClassWithMainMethod.class.getName());
      Process process = processBuilder.start();
      process.waitFor();
  }
  </code></pre>
  
  <p>Also, the currently running JVM might have been started with some other parameters (-D, -X..., ...) that the second JVM would not know about.</p>