Within Java code itself, what is the best way to gracefully close the current running app, but immediately open another Java app?
Asked
Active
Viewed 273 times
2
-
Once you close the current app, it's not possible to do anything else afterwards. May be you want to trigger another Java app just before your current is getting closed? Also, it depends on what you mean by "gracefully close". – asgs Feb 12 '14 at 18:52
-
Hmm, yeah what does "gracefully close" mean specifically – Caffeinated Feb 12 '14 at 19:02
-
1For a lack of a better phrase, used that term thinking it would convey properly. Gracefully close, properly close, "close without issues", etc. – saki2fifty Feb 12 '14 at 19:47
-
1So your first app would run forever until being closed. In java, app can be killed with brute force by killing enclosing process, or gracefully - when the app is programmed so that waits some signal to exit. Is your first app programmed in this way? If yes, then add another type of signal not just exit but only start another app inside the same process. – Alexei Kaigorodov Feb 12 '14 at 19:50
-
1And thanks for the link... Looks like its has what I'm looking for. I'd upvote... but cant. – saki2fifty Feb 12 '14 at 20:04
2 Answers
2
The best way for this to happen would be that the last 'step' in your program (the last piece of executable code) would be a Runtime.exec("Whatever.exe");
type command.
this way, immediately after your next program launches, your current program would finish it's current execution and 'gracefully' close. (that is, it closes without you explicitly saying something like System.exit(0)
)

WillBD
- 1,919
- 1
- 18
- 26
-
1Maybe adding a `ShutdownHook` would even make it more "graceful" in complex programs. – Sebastian Höffner Feb 12 '14 at 19:49
-
0
The following code will gracefully end the current program and immediately start another:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
Runtime.getRuntime().exec("java", "WhateverTheOtherJavaAppIsCalled");
}
});
System.exit(0);

tbodt
- 16,609
- 6
- 58
- 83