It's not a secret anymore!*
The moose likes Beginning Java and the fly likes What does System.exit(0); do? 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 "What does System.exit(0); do?" Watch "What does System.exit(0); do?" New topic
Author

What does System.exit(0); do?

Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
I have got a basic menu program at the moment, and I am compiling and running it in command prompt.

Problem is, i have declared the code to exit system when 2 is pressed. but all it does it quit the program and return to command prompt. Is this right? or is it meant to close cmd as well?
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
basically my quesiton is:

If i created an executable version of the program at the end, and i pressed 2 to quit the program, would it exit completely?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
it meant to close cmd as well?

No. The console window should stay open. The exit is for the JVM, not the JVM's calling program.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Originally posted by Waria Ahmed:
basically my quesiton is:

If i created an executable version of the program at the end, and i pressed 2 to quit the program, would it exit completely?


Most of the time, yes this would exit the application completely if the executable is double clicked on.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
It is all right to use System.exit(int) in a simple application with a single thread, but in a multi-threaded application it can be harmful; if you stop your JVM while a thread is still doing something, that something might only be half-done. You might have half a record written to a file, and that file will be corrupt for next time it is used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
And yes, it will close your application completely.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
I presume you have read the System class documentation?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does System.exit(0); do?
 
Similar Threads
Running Java Programs from the command prompt
anything similar to clscr() in java
Exception in thread "main" java.lang.NoClassDefFoundError: Test
JAR with no Command Prompt involved
Running a Java app on Windows XP