155

What's the best way to quit a Java application with code?

Kara
  • 6,115
  • 16
  • 50
  • 57
Olaseni
  • 7,698
  • 16
  • 44
  • 68

12 Answers12

201

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)

Community
  • 1
  • 1
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
108
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.

Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
  • I thought it was System.exit(1) ? – AFK Apr 19 '10 at 21:21
  • 14
    As 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
10

System.exit(int i) is to be used, but I would include it inside a more generic shutdown() method, where you would include "cleanup" steps as well, closing socket connections, file descriptors, then exiting with System.exit(x).

Kevin
  • 4,618
  • 3
  • 38
  • 61
Bastien
  • 1,537
  • 2
  • 10
  • 19
6

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().

Christian Semrau
  • 8,913
  • 2
  • 32
  • 39
4

Using dispose(); is a very effective way for closing your programs.

I found that using System.exit(x) resets the interactions pane and supposing you need some of the information there it all disappears.

Kevin
  • 4,618
  • 3
  • 38
  • 61
Joshua
  • 41
  • 1
3

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
Freiheit
  • 8,408
  • 6
  • 59
  • 101
1

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.

Vaishak Suresh
  • 5,735
  • 10
  • 41
  • 66
0

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;
Anton
  • 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
-1
Runtime.getCurrentRumtime().halt(0);
Dude Bro
  • 1,152
  • 1
  • 10
  • 13
  • 1
    Better: Runtime.getRuntime().halt(0); Worked perfectly for me within Scala! – Johann Hagerer May 27 '13 at 11:53
  • @GerhardHagerer but what if you get the not current runti,me? – Dude Bro Jun 25 '13 at 14:06
  • 1
    From 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
-2

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.

SmartFingers
  • 105
  • 1
  • 8
-3

System.exit(ABORT); Quit's the process immediately.

-3

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();
    }
}