• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the proper way to exit a Java application?

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?



 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic