tags:

views:

306

answers:

7

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 ?

+1  A: 

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.

Harsha
Only if main() is void.
bkritzer
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
ya right, only if main() is void.
Harsha
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
@Steven: Thanks for the info.
Harsha
A: 

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.

Adamski
+1  A: 

Here's all I can think of off the top of my head:

  1. main() returns (either a value or a void() main finishes executing its last statement)

  2. program throws an exception uncaught

  3. System.exit(int)

  4. 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.

bkritzer
In reference to 1.: If there are non-deaemon threads running, the program will continue to run even after main() has returned.
jeremytrimble
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`
R. Bemrose
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_D
@Andreas_D Cool. Learn something new every day.
bkritzer
+3  A: 

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

Tim Jansen
Nit-picking, but what if you've register a shutdown hook that enters an infinite loop when it runs?
Adamski
A: 

You can use Application.exit() as well.

GK
Pretty sure, 'Application' is some framework class and not part of the JRE - could you provide the full qualified classname?
Andreas_D
+2  A: 

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 :)
Merlyn Morgan-Graham
ROFL. +1 for that one
DVK
I'm not entirely joking :) This is actually an important scenario for a server that needs to fail without losing data.
Merlyn Morgan-Graham
+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
http://thedailywtf.com/Articles/My-Tales.aspx
Meinersbur
A: 

You can end a running Java program externally. By killing the java.exe process from task manager (in case of windows)

Bragboy