What's the best way to quit a Java application with code?
12 Answers
You can use System.exit()
for this purpose.
According to oracle's Java 8 documentation:
public static void exit(int status)
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
This method calls the exit method in class Runtime. This method never returns normally.
The call
System.exit(n)
is effectively equivalent to the call:
Runtime.getRuntime().exit(n)

- 1
- 1

- 62,090
- 32
- 125
- 150
System.exit(0);
The "0" lets whomever called your program know that everything went OK. If, however, you are quitting due to an error, you should System.exit(1);
, or with another non-zero number corresponding to the specific error.
Also, as others have mentioned, clean up first! That involves closing files and other open resources.

- 17,276
- 9
- 52
- 70
-
-
14As Chris states, `System.exit(1)` (or any other number) is if you want to indicate that it closed due to an error. `System.exit(0)` indicates that the program closed normally. You can also change 1 to any number you like, then when you are running your application from a script you can determine if there was an error. – StormFoo Nov 24 '11 at 12:59
System.exit()
is usually not the best way, but it depends on your application.
The usual way of ending an application is by exiting the main()
method. This does not work when there are other non-deamon threads running, as is usual for applications with a graphical user interface (AWT, Swing etc.). For these applications, you either find a way to end the GUI event loop (don't know if that is possible with the AWT or Swing), or invoke System.exit()
.

- 8,913
- 2
- 32
- 39
I agree with Jon, have your application react to something and call System.exit().
Be sure that:
- you use the appropriate exit value. 0 is normal exit, anything else indicates there was an error
- you close all input and output streams. Files, network connections, etc.
- you log or print a reason for exiting especially if its because of an error

- 8,408
- 6
- 59
- 101
The answer is System.exit(), but not a good thing to do as this aborts the program. Any cleaning up, destroy that you intend to do will not happen.

- 5,735
- 10
- 41
- 66
There's two simple answers to the question.
This is the "Professional way":
//This just terminates the program.
System.exit(0);
This is a more clumsier way:
//This just terminates the program, just like System.exit(0).
return;

- 35
- 4
-
`System.exit(0); return;` is good too, the return prevents any ruther code from being run before the System can exit, otherwise you'd need a flag (.. `i.e. if (!called_exit) { further code after exit statement }` – ycomp Apr 15 '16 at 00:36
Runtime.getCurrentRumtime().halt(0);

- 1,152
- 1
- 10
- 13
-
1Better: Runtime.getRuntime().halt(0); Worked perfectly for me within Scala! – Johann Hagerer May 27 '13 at 11:53
-
-
1From the Javadoc for `halt`: "This method should be used with extreme caution. Unlike the `exit` method, this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated then this method does not wait for any running shutdown hooks or finalizers to finish their work." In short, you shouldn't need to use `halt`, you should prefer `exit`. – Erwin Bolwidt Jun 11 '17 at 03:05
System.exit()
will do what you want. But in most situations, you probably want to exit a thread, and leave the main thread alive. By doing that, you can terminate a task, but also keep the ability to start another task without restarting the app.

- 105
- 1
- 8
System.exit(ABORT); Quit's the process immediately.
This should do it in the correct way:
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowListener() {
@Override
public void windowClosing(WindowEvent e) {
if (doQuestion("Really want to exit?")) {
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.dispose();
}
}