File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.exit(0);" Watch "System.exit(0);" New topic


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

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

Joined: Jul 22, 2000
Posts: 9059
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.

"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.
I agree. Here's the link:
subject: System.exit(0);
It's not a secret anymore!