简体   繁体   中英

Start separate process to run Java program using same JRE

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?

Javapackager from Java version 9 on includes the bundler argument -strip-native-commands which leaves the executables in the bundled JRE. Just include the option:

-Bstrip-native-commands=false

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM