This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System.exit(0); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.exit(0);" Watch "System.exit(0);" New topic
Author

System.exit(0);

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Why dos programs don't need to call System.exit() and windows programs need to call System.exit()???
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
I was working through an example program that called System.exit(). I commented it out. Assuming you are not running a complicated multi-threaded program, when the program goes out the bottom of main(), the JVM will take care of the shut down sequence.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
A java program terminates when there is no more non-daemon thread running. The AWT event handling thread is a non-daemon thread, so you either have to stop it (though I don't recall how to do that) or to call System.exit().


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Benjoe Reyes
Ranch Hand

Joined: Apr 24, 2002
Posts: 109
in an applet you don't need to call System.exit(o) because the program terminates when you shut down the applet. but an application doesn't do so... if you closes the window press the 3-fingered salute (ctrl+alt+del) and you will see a java application still running. it'll cost your ram. so call System.exit(0) use the windowsadapter so when you close the window it also shuts down the program
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
It is not a function of whether it is an applet or an application. It is a function of whether it is a GUI (AWT or Swing) program or not.

If it is not a GUI program, the program will end when all the non-daemon threads end (usually main() although not always). System.exit() is not required.

In an applet, the browser closes the GUI program.

In a GUI application you, the programmer, have to close the program by using System.exit().

Actually, if I recall correctly, Swing has this functionality built in but the older AWT stuff does not.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
Swing added setDefaultCloseOption(EXIT_ON_CLOSE) to JFrame in Java 1.3.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: System.exit(0);
 
Similar Threads
Program wont terminate
Timer keeps program from exiting
Number of JVMs in a CPU
System.exit(0) necessary after JOptionPane???
System.exit() closes JVM