4
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.

Saturn
  • 17,888
  • 49
  • 145
  • 271
  • Possible duplicate of [this question](http://stackoverflow.com/questions/931536/how-do-i-launch-a-completely-independent-process-from-a-java-program). Check whether that helps you out. Note that it looks like it is platform dependent, so what platform are you using? – Engineero Jun 09 '14 at 22:38
  • What OS are your using? – MadProgrammer Jun 09 '14 at 22:41
  • Windows 7, sorry forgot to mention. – Saturn Jun 09 '14 at 22:43

3 Answers3

5

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 
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • Hm, with the Windows line, it says that it cannot find my program: `Runtime.getRuntime().exec("start /min java -jar someOtherProgram.jar");` – Saturn Jun 09 '14 at 22:45
  • check my updated answer. You may want to try javaw (javaw.exe) – Emanuel Jun 09 '14 at 22:46
  • @EmanuelSeibold I think you'll find javaw will have the same effect as using java, this is how the OS chains processes together, meaning the current process can't end till any child process terminate... – MadProgrammer Jun 09 '14 at 22:49
  • @JustKidding You should use ProcessBuilder and set the "directory" property to set the start location of the program – MadProgrammer Jun 09 '14 at 22:50
  • MadProgrammer, http://stackoverflow.com/questions/1997718/difference-between-java-exe-and-javaw-exe "javaw.exe is the command which will not wait for the application to complete." means the shell dont know it's still running even when it think it's already finished. – Emanuel Jun 09 '14 at 22:51
  • @EmanuelSeibold Yep, but that's not the same thing as a parent->child process chain, we're not talking about a shell here ;) – MadProgrammer Jun 10 '14 at 00:01
  • @EmanuelSeibold Owww, you might be right (about JavaW) - wasn't able to get this to work under Java 6 for some reason, nice ;) – MadProgrammer Jun 10 '14 at 00:12
  • @madprogrammer there's nothing in General which would prevent a parent process from exiting while child processes are still running. In this case the OP was using the java command which intentionally waited for the child - but it's not the OS forcing that. – Nerdtron Jun 12 '14 at 03:46
3

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"
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

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

System.exit(0);
saclyr
  • 161
  • 5