File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes System.exit(0) necessary after JOptionPane??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "System.exit(0) necessary after JOptionPane???" Watch "System.exit(0) necessary after JOptionPane???" New topic
Author

System.exit(0) necessary after JOptionPane???

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Another question: my book says I must use System.exit(0) after using JOptionPane, but I've constructed a few programs now without using this call, and they work fine. In what cases must I use System.exit(0), then? Thanks in advance!

-Patrick


Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
The usual explanation is the every time you use JOptionPane you start a new Thread; using System.exit(0) when the application has completely finished ensures that the JVM exits and all those Threads are actually killed.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
OK, so, coming from C++, I guess one could liken it to destroying a pointer after use, to free up memory?

-Patrick
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
From Patrick Brooks:
OK, so, coming from C++, I guess one could liken it to destroying a pointer after use, to free up memory?
No. I am not sure quite how essential the instruction is, but it is not at all like freeing a pointer. There is no explicit memory freeing action in Java at all; memory management is all done behind the scenes and users are barely aware of it.

Use of System.exit(0) in this instance is intended to make sure the virtual machine shuts down properly.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Thanks for the clarification!

-Patrick
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
A book says you MUST use System.exit() after using a JOptionPane?! Either the context is very different or that's a very bad book.

System.exit() is very rarely really needed. A Java program will exit just fine, if you ensure that all threads exit (i.e. the method in which they started returns).

Using System.exit() liberally throughout your code is bad for code re-use. Say you write a method and put System.exit() in its error handling, because the application in which you first use that method wants to exit on error. But then later you want to re-use the method in a Web application. Your System.exit() will kill the whole Web server, when really you just wanted it to log a problem and carry on.

An additional problem with System.exit() occurs in any multi-threaded application. System.exit() kills all threads pretty much straight away. But some threads might be doing something critical like updating a file. The file could get corrupted if thread is killed at a bad time. Instead, all threads should be allowed to finish in their own time. An application should have some way of signalling to all threads that shut-down is wanted, then they can shut-down safely.

The specific issue with JOptionPane may be one that afflicts all Swing (and AWT?) graphical applications. A Swing application will not exit until all windows are disposed. But if you create a top-level window without specifying any parent for it, Java will create a Parent that you can't control and hence never gets disposed. Instead, when creating a top-level window, you should give an explicit parent, such as an invisible JFrame. When you want to exit, you should dispose() the invisible JFrame, then the app will exit.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Campbell Ritchie:
The usual explanation is the every time you use JOptionPane you start a new Thread


I'm a bit rusty on this, but am fairly sure that's not true.

When you first use Swing code in a Java application, a thread (or threads?) get started to service the Swing subsystem. But I'm pretty sure you don't get an additional thread for every JOptionPane.

See my earlier response for why people sometimes use System.exit() in Swing applications, why they mostly shouldn't, and what they should do instead.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
That is the explanation given for using System.exit(0) when you have used JOptionPane. Thank you, Peter Chase, for pointing out it is mistaken advice.
 
 
subject: System.exit(0) necessary after JOptionPane???
 
Similar Threads
Help w/ Data Input Error
Hashing Index
modal in jdk1.3 & Win2000
Window Close Conformation
Probably a stupid question but...