I want to start a separate process from my java program to run another java program using same JRE that the current java program is executing in. Normally, I could get the path to the java executable using System.getProperty, but the java program is running in a bundled jre (Mac app package) which doesn't actually contain a java executable. Therefore, I'm wondering if there is there any API to directly run a Java program in a separate process?
Add a comment
|
3 Answers
The API is public hosted here: http://docs.oracle.com/javase/8/docs/api/
and the information you are looking for cons from the System utility class:
All available properties are listed here: http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties--
The current JVMs location is available via "java.home".
So what your looking for is:
String javaPath = new File( System.getProperty("java.home"),"bin/java").absolutePath();
5 Comments
shmth
As I mention, the bundled jre doesn't actually contain a java executable
Timothy Truckle
@SimonZhu "As I mention, the bundled jre doesn't actually contain a java executable" - This does not matter. The program was started with a JRE installed on your system, and from within java this property always points to the currently running Java installation, regadless of where your system wide, or shell specific
$JAVA_HOME points to.shmth
No, the program runs in the bundled jre, which doesn't contain a java executable. In other words,
System.getProperty("java.home") + "bin/java" does not exist.Timothy Truckle
@SimonZhu "No, the program runs in the bundled jre, which doesn't contain a java executable." that's nonsense. no java executable -> no JRE end of message!
This may give a better picture.
Get the Java executable using below.
System.getProperty("java.home") + "/bin/java"
ReConstruct the class path,
((URLClassLoader() Thread.currentThread().getContextClassLoader()).getURL()
From here, you can start the new process using
Process.exec(javaExecutable, "-classpath", urls.join(":"), CLASS_WIH_MAIN)
1 Comment
shmth
As I said to Timothy and I mention in my question, the bundled jre doesn't contain an executable java. The filepath returned by
System.getProperty("java.home") does not even contain a bin folder.