37

What is the difference between these two methods - System.exit() and JFrame.dispose()?

If we want to close a Java Swing application when a button is clicked, which method should I use?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Adil
  • 4,503
  • 10
  • 46
  • 63
  • on button click. is about standalone JButton with System.exit(0) or from button in JFrames ToolBar – mKorbel Nov 13 '12 at 12:09
  • `JFrame.dispose()` [triggers a bug](https://stackoverflow.com/questions/2225737/error-jdwp-unable-to-get-jni-1-2-environment), it seems `System.exit()` is advised –  Oct 16 '20 at 17:26

6 Answers6

63

System.exit(); causes the Java VM to terminate completely.

JFrame.dispose(); causes the JFrame window to be destroyed and cleaned up by the operating system. According to the documentation, this can cause the Java VM to terminate if there are no other Windows available, but this should really just be seen as a side effect rather than the norm.

The one you choose really depends on your situation. If you want to terminate everything in the current Java VM, you should use System.exit() and everything will be cleaned up. If you only want to destroy the current window, with the side effect that it will close the Java VM if this is the only window, then use JFrame.dispose().

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • The problem I see with System.exit is that it really kills the application, even if a thread is, for example, writing a file. It seems to me that most applications close the window, than wait for rest of the threads to complete. But as the AWT threads may not complete, we've got a problem how to implement this in Java properly. – Tomáš Zato May 07 '15 at 08:30
  • thanks for the documentation. I was looking for this one – Andy McRae Mar 04 '19 at 17:50
  • @TomášZato-ReinstateMonica That thread would then mean the VM never exits, either. The solution is to ensure that the 'close' button is not accessible. System.exit is what you should call if your intent is for the app to be shut down. A combination of ensuring any UI elements that lead to this are disabled for the duration, + Runtime's addShutdownHook are for cleanup. – rzwitserloot Dec 26 '20 at 15:43
  • Thanks indeed for the documentation. https://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose() goes straight to what I think is the most relevant bit – Martin Dorey May 28 '22 at 16:25
12

JFrame.dispose()

public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable. The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.

System.exit()

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)
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
6

In addition to the above you can use the System.exit() to return an exit code which may be very usuefull specially if your calling the process automatically using the System.exit(code); this can help you determine for example if an error has occured during the run.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
5
  • If you have multiple windows open and only want to close the one that was closed use JFrame.dispose().

  • If you want to close all windows and terminate the application use System.exit()

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
Noah Herron
  • 630
  • 4
  • 23
0

JFrame.dispose() affects only to this frame (release all of the native screen resources used by this component, its subcomponents, and all children). System.exit() affects to entire JVM.

If you want to close all JFrame or all Window (since Frames extend Windows) to terminate the application in an ordered mode, you can do some like this:

Arrays.asList(Window.getWindows()).forEach(e -> e.dispose()); // or JFrame.getFrames()
ggrandes
  • 2,067
  • 22
  • 16
0

If you terminate your programme then you use System.exit(0);

On the other hand,you have multiple window,,but you can terminate & close only one window then you use dispose();

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 22 '21 at 08:51