I know use System.exit(0) can end a java program, for instance, if I have a JFrame window, it will close and end the program, but I wonder how many other ways, can it be closed and the program be ended ? Including when an error occurs, will the program be shut down and the JFrame be closed ?
7 Answers
To add to other answers:
- If the process that is hosting the VM is forcefully terminated, your program will spontaneously disappear
- The same happens if the plug gets pulled on the machine hosting the VM :)

- 58,163
- 16
- 128
- 183
-
1I'm not entirely joking :) This is actually an important scenario for a server that needs to fail without losing data. – Merlyn Morgan-Graham Feb 04 '10 at 19:40
-
1+1 that's one of the nastiest scenarios. If it weren't for that one, most high-availability software could be **much** faster. – Joachim Sauer Apr 07 '10 at 23:51
A Java program ends when the last Thread without daemon flag ends, or when you call a method that shuts down the virtual machine (System.exit(), Runtime.exit(), Runtime.halt() and possibly a few more).
Anything else is up to libraries that call System.exit() (such as a JFrame with EXIT_ON_CLOSE).

- 3,330
- 2
- 23
- 28
-
1Nit-picking, but what if you've register a shutdown hook that enters an infinite loop when it runs? – Adamski Feb 04 '10 at 18:44
Here's all I can think of off the top of my head:
main() returns (either a value or a void() main finishes executing its last statement)
program throws an exception uncaught
System.exit(int)
It can crash?
In your case of a JFrame closing, I believe there would be an onClose() handler which either calls System.exit(0) or causes the main method to return.

- 1,418
- 3
- 16
- 27
-
2In reference to 1.: If there are non-deaemon threads running, the program will continue to run even after main() has returned. – jeremytrimble Feb 04 '10 at 18:41
-
JFrames have a method named `setDefaultCloseOperation` that takes one of these constants as an argument: `WindowConstants.HIDE_ON_CLOSE` (the default), `WindowConstants.DO_NOTHING_ON_CLOSE`, `WindowConstants.DISPOSE_ON_CLOSE`, and `JFrame.EXIT_ON_CLOSE` – Powerlord Feb 04 '10 at 18:44
-
Please have a look at this answer regarding the end of main(): http://stackoverflow.com/questions/2070951/main-function-does-not-return-anything-why/2070968#2070968 – Andreas Dolk Feb 04 '10 at 18:54
-
One other way the Java program ends is when the last statement in the java code is executed. Also when java.lang.OutOfMemory error occurs, the program terminates abnormally. This occurs when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

- 5,291
- 13
- 43
- 47
-
What does this mean? What if the last line of code being executed by a given thread (e.g. within a Runnable) is executed but there are other non-daemon threads alive? – Adamski Feb 04 '10 at 18:34
-
OutOFMemoryErrors are not necessarily fatal, nor do they necessarily terminate the JVM. It will only terminate if all non-daemon threads terminate. The normal case is that OOME will happen on one thread, killing it and freeing the heap of stack-referenced things from that thread. Then, either there will be enough memory to continue or another thread will die. This repeats until either you can make forward progress or you've killed all threads, causing the JVM to terminate... – Steven Schlansker Feb 04 '10 at 18:57
I've answered your question in the context of Swing GUIs given your mentioning of JFrame
.
- With a Swing GUI the Event Dispatch thread will log any exceptions that occur but will not terminate the application in this situation.
- Similarly, if another thread throws an exception and terminates the Event Dispatch thread will ensure that the application is kept alive (as it is not a daemon thread).
- One final point: If you wish your application to be terminated when a JFrame is closed, you need to call:
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Otherwise the frame will simply be hidden when you close it, but the application will continue to run.

- 54,009
- 15
- 113
- 152
You can end a running Java program externally. By killing the java.exe process from task manager (in case of windows)

- 34,892
- 30
- 114
- 171
You can use Application.exit() as well.

- 25,776
- 50
- 140
- 201
-
Pretty sure, 'Application' is some framework class and not part of the JRE - could you provide the full qualified classname? – Andreas Dolk Feb 04 '10 at 18:50