Saturn
Saturn

Reputation: 18149

How can I run a new process, and close the current program in 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.

Upvotes: 4

Views: 2248

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347204

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"

Upvotes: 3

Emanuel
Emanuel

Reputation: 8106

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 

Upvotes: 5

saclyr
saclyr

Reputation: 161

The only way that comes to my head is to use the exit() method. It shuts down itself.

System.exit(0);

Upvotes: 1

Related Questions