tags:

views:

44

answers:

2

How does one obtain the location of the executable of the currently running JVM during runtime? I would like to instantiate another JVM as a subprocess using the ProcessBuilder class.

I am aware that there is the java.home System property, but this doesn't specify the location of the JVM executable. I understand I could do something like this to get the path:

System.getProperties().getProperty("java.home") + File.pathSeparator + "bin" + File.pathSeparator + "java"

This code isn't platform independent, because the Windows executable's name is java.exe, not java. Is there a way to get the path of the JVM executable that takes the platform's idiosyncrasies into account?

A: 

You are trying to fork the entire JVM.

  1. That is extremely inefficient, mainly because of the heaviness of yet another java process. If your heavily doing this, then your program is going to be really slow
  2. Threads exist for this very reason

But if you really must, you can try just executing java -arguments directly, since most standard java installations put java on the cli path.

TheLQ
The question doesn't say anything about launching so you should probably say "if you are using java.exe to launch beware...". The question is only about getting the running java exe.
jowierun
In addition to what jowierun wrote, there *are* benefits to forking a JVM:1. no class loading conflicts between JVMs -- this is important for large applications2. when running an expensive algorithm, running it as a separate process is a good idea; it gets around the memory limitations
Samad Lotia
A: 

Yes, there is a way to get the path of the JVM executable (if it exists). Include it in the configuration of the application. There are lots of ways to do that: Command line argument -- java myApp.Main /path/to/Java; Properties -- java -Dpath.to.java=/path/to/java; etc.

If you want true platform independence, then your whole scheme is flawed because the existence of a JVM executable is not guaranteed. I could imagine a JVM with no need for a java executable.

If you want 99.99% platform independence, then I think you have the tools needed.

emory
This doesn't answer my question. I am asking how to determine the location of the JVM executable *at runtime*, not a priori before the JVM is started. You assume is that I'm in control of how the JVM is invoked, and this isn't the case.
Samad Lotia