aspose file tools*
The moose likes Java in General and the fly likes What is the proper way to exit a Java application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the proper way to exit a Java application?" Watch "What is the proper way to exit a Java application?" New topic
Author

What is the proper way to exit a Java application?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Hi,

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

What is a better way to quit a program?



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
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.

Anybody else?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the proper way to exit a Java application?