• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.exit(0)  RSS feed

 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
"System.exit(0)"-->It means that it terminates the currently running Java Virtual Machine with "0" as status(i.e, sucessful termination). what does that "status" refers to?
Even its working for System.exit(2) or for any status value.. could anyone clear that? Thanks in advance

-Thanks & Regards,
Hamsa
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no common definition of what the status means. Non-zero values indicate faulty or abnormal termination, as the javadocs explain. The status could be used by the shell script or OS process that started the JVM to differentiate between various outcomes. But the fact that a process communicates a status to the OS originates in Unix; I'm not even sure if you can get at that value under Windows or other OSes.
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
There is no common definition of what the status means. Non-zero values indicate faulty or abnormal termination, as the javadocs explain. The status could be used by the shell script or OS process that started the JVM to differentiate between various outcomes. But the fact that a process communicates a status to the OS originates in Unix; I'm not even sure if you can get at that value under Windows or other OSes.


Thank you ulf.

-Thanks & Regards,
Hamsa
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General.

Hamsagayathri Palanisamy,
Please try to use the SCJP forum for questions related directly to the test only.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
I'm not even sure if you can get at that value under Windows or other OSes.


Yes, on Windows, it goes in the ERRORLEVEL environment variable. And Mac OS X behaves like Unix in this respect.

Therefore, the most common desktop O.S. are all covered.

This said, I personally think System.exit() is used much too much. Wherever possible, your Java applications should exit "naturally". By that I mean that all the non-daemon threads should finish cleanly, which allows the JVM to exit.

Whenever System.exit() is called, there is a possibility that some thread is in the middle of something important. Even if your code is single-threaded, some library call may have run something on another thread.

Further, code that calls System.exit() is difficult to re-use directly, because it may choose to exit at inappropriate times. For instance, if you try to use such code in a Web application server, the System.exit() will exit the whole server, not just your app (or maybe you'll get SecurityException, preventing exit).

System.exit() is necessary when you really need to return a exit status code to the calling program. There is, unfortunately, no other way to set the exit status code.

Note that people writing Swing and AWT code often think they need to use System.exit() to force it to exit. This is not the case. Proper coding (nothing difficult) will allow it to exit naturally.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!