I have a desktop app and when someone presses a button I want it to kick off another JVM that executes a class’ main method. My desktop app already depends on the jar that contains the class with the main method that I want to execute.
Currently I’ve got the following code, however, I was hoping their was a more elegant way of doing this:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java -jar another.jar");
I know I can use ProcessBuilder too.
Is there no way such as (excuse the pseudo code):
Jvm.execute(Main.class);
Since the Main class that I want to call already exists in my classpath, it just feels weird to have to run the java command via Runtime.
Very good question. Try to search into management API: http://cupi2.uniandes.edu.co/javadoc/j2se/1.5.0/docs/api/javax/management/package-frame.html
Good luck.
I am not sure that this API exists, but if it is it should be there.
I’d personally used ProcessBuilder as you but specify explicit path to java by retrieving system properties of current process.