• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding System.exit(int n)

 
Keshava murthy S
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by invoking this method with an integer argument ranging between 1 to 14 we can shut down the runnibng JVM. What i know is i have to use only arguments between 1 to 14 , and they carry some meaning, and my question is what actually these constants state or represent.


thanks in advance
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find that information, about that the argument should be in the range 1 to 14?

It doesn't matter what the argument is. System.exit(int n) terminates the JVM, and the value of the argument doesn't matter.

The argument is just the exit code that the JVM passes back to the operating system when the process ends. On for example Unix, you can write a shell script in which you can retrieve the exit code and use it in any way you like.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper says, the argument to System.exit() can be any integer.

If you are running your application inside some outer program (Java or non-Java), then that outer program might impose restrictions on the exit code. Otherwise, there are no restrictions.

A very common exit code is zero. This is often interpreted by the program that ran your Java program as a successful exit, whereas non-zero is often considered to be an error code. However, this is not universally true, and you need to find out what is the case for your environment. It's nothing to do with Java, though; the same would apply to any program.

A last thought: Java programs should not use System.exit() at all, unless they really have to. A Java program will exit just fine when all the non-daemon threads have exited. It is much better to exit this way, as you can be sure that all threads have had a chance to do any necessary clean-up. If you System.exit(), some thread might be in the middle of some operation. Also, code that calls System.exit() is difficult to incorporate into a larger application (e.g. a Web application server), as System.exit() kills the whole process, not just its part of it.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

We can pass any integer number to System.exit() method. Mostly the negative numbers represent the failure of the operation , zero success and positive number some information messages to the OS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic