I have the following code to quit a Java application that runs on desktop and on laptop computers. A Java software analysis tool called "FindBugs" informed me that using System.exit() is not the best way to exit a Java application because it exits the entire JVM and not just the program that calls exit().
If at all possible, simply get to the end of all your methods and let all your threads die. That is the simplest way to exit an application.
System.exit(i) is a bit vicious; the problem you can get is that if you call it in one thread, then all the other threads will stop too, and you may end up with loose ends, or files not completely written, or something. Using EXIT_ON_CLOSE on a JFrame can be a bit vicious too.
You really want to clear up all the loose ends in every thread, let all their while loops finish, finish any writing to files etc, then let all the non-daemon threads die.
I agree with Campbell's points in general. For a JFrame, try using
DISPOSE will make the resources of that window available for GC - and if that is the last AWT/Swing component on the screen, it will also shut down the swing event processing thread. This will allow the JVM to exit if there are no other non-daemon threads still running (just like the JVM normally exists, when you're not using AWT/Swing components).
Kaydell Leavitt wrote:... using System.exit() is not the best way to exit a Java application because it exits the entire JVM and not just the program that calls exit().
Yes, that can be a problem. Especially when you write a Java library (a JAR file to be used by applications) or in a Java EE application, using System.exit() is a big no-no. Imagine that you have a method in a library that calls System.exit() - it would immediately kill the whole application. In a Java EE application it could be even worse, it could just stop the whole application server and make your website go offline.
I've worked on a project where we created a Swing application. Later, the client decided to make that application run inside an application container they had written themselves. The container was just another Java program that calls the main() method of our Swing application. This is an example of where calling System.exit() in our Swing application would have been a problem, because it would stop the whole JVM, including the application container, instead of just stopping the Swing application.