Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the proper way to exit a Java application?

 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic