views:

571

answers:

2

This is a followup to my own previous question 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:

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();
}

Also, the currently running JVM might have been started with some other parameters (-D, -X..., ...) that the second JVM would not know about.

+4  A: 

It's not clear to me that you would always want to use exactly the same parameters, classpath or whatever (especially -X kind of stuff - for example, why would the child need the same heap settings as its parents) when starting a secondary process.

I would prefer to use an external configuration of some sort to define these properties for the children. It's a bit more work, but I think in the end you will need the flexibility.

To see the extent of possible configuration settings you might look at thye "Run Configurations" settings in Eclipse. Quite a few tabs worth of configuration there.

djna
A config file for the JVM is a good idea. I think it should be editable in a dialog so that users can but need not tweak the parameters (much like the Eclipse example). Thanks for that.What I was also aiming at was the path to the executable. As Stephen C mentioned the code will not work if the executable of the JVM is not called "java". Is there any system-independent way to find the executable name?
Robert Petermeier
As it happens I'm currently working with a java whose executable is not "java" by name. And I'm spawning from with a traditional Java. And I have at least 4 different java executables onmy machine, and to "not java" javas. You can't deduce the reliably, so why not include the path to the java as one of the configurable items. You will see that this is actually what Eclipse does, albeit in a not very easy to configure way.
djna
+2  A: 

I think that the answer is "Yes". This probably as good as you can do in Java using system independent code. But be aware that even this is only relatively system independent. For example, in some systems:

  1. the JAVA_HOME variable may not have been set,
  2. the command name used to launch a JVM might be different (e.g. if it is not a Sun JVM), or
  3. the command line options might be different (e.g. if it is not a Sun JVM).

If I was aiming for maximum portability in launching a (second) JVM, I think I would do it using wrapper scripts.

Stephen C
Thanks for that. The JAVA_HOME variable is not relevant here as System.getProperty("java.home") != JAVA_HOME. Do you have an example of a JVM where my code would not work because the executable has a different name?
Robert Petermeier
@Robert: a couple of examples: IBM (and others?) have "javaw" as an alternative, and Jikes RVM is launched using "rvm". (And for JNode, you could run "java" or "org.jnode.command.common.java" ... commands are not identified by pathnames.)
Stephen C