Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a new process, and close the current program in Java?

Tags:

java

public class App {
    public static void main(String[] args) {
       Process p = Runtime.getRuntime().exec("java -jar someOtherProgram.jar");
    }
}

This effectively runs my other program. However, this program (App) does not close. It is still running (I can tell because it won't let me kill it in Eclipse). The only way to close this program is by killing the process that corresponds to someOtherProgram.jar.

What I want is for my program, App, to run another Java program. And then kill itself.

like image 744
Voldemort Avatar asked Jan 11 '23 10:01

Voldemort


2 Answers

You may want to use (on Linux)

nohup java -jar someOtherProgram.jar & 

and in Windows

start /min java -jar someOtherProgram.jar

or

javaw -jar someOtherProgram.jar 
like image 71
Emanuel S Avatar answered Jan 28 '23 17:01

Emanuel S


Under Windows, I believe you need to use something like...

cmd /B start "" java -jar someOtherProgram.jar

Remember, if your path contains spaces, it will need to be quoted, for example

cmd /B start "" java -jar "path to your/program/someOtherProgram.jar"
like image 43
MadProgrammer Avatar answered Jan 28 '23 17:01

MadProgrammer