Do you want to get absolute path of the specific Java executable that was used to launch a program, from within that program itself? May be you want to launch another Java program P2 from a program P1 and you want to make sure that P2 is launched using the same version of Java that was used to launch P1?
It is possible. Java runtime makes available a system property java.home which contains the path to its installation or root directory. Java executable is usually located (tested on Windows and Mac) inside java.home in a folder named bin . We can combine these two facts to get absolute path of the Java executable that was originally used to launch a program. Here is a code snippet that does exactly this:
String javaHome = System.getProperty("java.home");
String javaExe = javaHome + File.separator + "bin" + File.separator + "java";
You will need to import java.io.File in your source code to compile it. After these couples of lines have executed, javaExe will contain our required path. You can use it for whatever purpose you want. Here is a sample code snippet that uses this Java executable to launch another Java program in a separate JVM. Continue reading