0

That is the question: my Java application is running, so how to get the path/name to the java binary that is running it? For example, 'C:\Program Files\Java\jre6\bin\java.exe' in Windows or '/usr/bin/java' in Linux. I need a platform independent solution, preferably relying only on the JDK.

The context for the question is the following. I'm developing two Java applications, let's say FirstApplication and SecondApplication. The FirstApplication will start the SecondApplication using the ProcessBuilder API. The problem is that I need to tell the path to the Java binary to start the SecondApplication. For example,

String cmd = "C:\Program Files\Java\jre6\bin\java.exe -cp <second application classpath> com.secondapplication.MainClass";
Process p = new ProcessBuilder(cmd).start();

Possible alternative solutions to this could be assume that there is a java binary available through the PATH system variable, or assume that the JAVA_HOME system variable is set. But this may not be always the case. Also, the final user of the application (targeting to other Java developers, as well as final users) may have more than one JVM installed on his system, and may execute the FirstApplication expliciting what JVM he wants. Ideally, the SecondAplication should run using the exact same Java binary as the FirstApplication, without relying on such system variables.

Thank you

lgfischer
  • 1,438
  • 2
  • 10
  • 17
  • 2
    Use java.home property. A duplicate of this post: http://stackoverflow.com/questions/4531626/get-path-to-java-jre – vaisakh Jul 08 '12 at 18:13
  • Ok, I got it. But still not sure about one point: what if the user executed using "javaw" instead of "java", or anything else that the executable may be named? – lgfischer Jul 08 '12 at 18:34
  • 1
    For `javaw` see http://stackoverflow.com/questions/2469047/can-i-find-out-if-the-java-program-was-launched-using-java-or-javaw – trutheality Jul 08 '12 at 18:57

0 Answers0