1

I've been learning Java for some months now and I came across this:

System.exit(int status);

I think you can use it to test your code depending on what "value" you give to it.

e.g if I want to check if a loop was used or worked as it should , I could do:

if(value == 1){
    System.out.println("Hi");
    System.exit(0);
}

But after a bit of testing it came out that I actually cant see the "value" after the loop executed.

Am I thinking of this right? Is there any other use of this code?

6
  • Related with System.exit() values: stackoverflow.com/questions/2434592/… Commented Jun 17, 2015 at 17:56
  • Yup, i already checked these! Commented Jun 17, 2015 at 18:00
  • All processes in every common OS return an 'exit code' when they terminate. This code is usually 0 if the process was successful, or some other process-specific code if the process failed (exception thrown, invalid arguments, etc.). The value to the System.exit function is that code. Commented Jun 17, 2015 at 18:02
  • Thanks! I think i get this code's use. Commented Jun 17, 2015 at 18:05
  • 2
    System.exit() shuts down the VM no matter if there are still threads running or how deeply nested the call stack is. Its the "get me out of here" call. The return code has no meaning to the VM itself - but its a simple and universally supported way of communicating that value to whatever environment started the VM. E.g. in a shell script, the script can act depending on the return code from the VM. Commented Jun 17, 2015 at 18:17

6 Answers 6

7

System.exit(value) terminates the JVM and uses value as the return value of the process. So, for instance, in *nix systems, you could use:

$ echo `java MyJavaClass`  
Sign up to request clarification or add additional context in comments.

1 Comment

So i need echo to display (print) it's value? cause i am testing it in eclipse and no value is being printed.
2

The java.lang.System.exit() method terminates the currently running Java Virtual Machine.

The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

Likewise, a 0 status code returns a normal termination;

System.exit(0) //exit signaling the process terminated successfully

System.exit(-1) //exit signaling the process did not terminate successfully

Comments

1

To check the exit code in Eclipse, switch to the "Debug" view. For example, immediately after running:

public class Test {
  public static void main(String[] args) {
    System.exit(55);
  }
}

the top left window in the Debug view contains:

<terminated>Test [Java Application] 
    <terminated, exit value: 55>C:\Program Files\Java\jre1.8.0_31\bin\javaw.exe (Jun 17, 2015, 11:21:15 AM) 

Comments

0
System.exit(value)

will cause the program to terminate with the given value. Traditionally (on unix systems at least), 0 has been designated as the numeric value that will indicate successful execution of a program. All non-zero values indicate a failure mode that the calling environment will know how to interpret.

I think you can use it to test your code depending on what "value" you give to it.

I wouldn't use this methodology to test my code. Instead consider writing unit tests to test your code.

Comments

-1

The documentation would (as usual) help a lot -.- . System.exit terminates the JVM.

4 Comments

i couldnt figure it out thats why i asked, is that all it does ? cause i read that the value you use actually does something
@Bill as any other program, the return-value is 0, if the program was terminated on purpose, or some other value, if an error occured during runtime
yeah i got this but if i use this code in eclipse and run it no value will be printed , so all it does is to terminate the programm and all i need is a "break;" and this code (System.out()) is useless.
@Bill i guess the console will log something like "process terminated with exit-code ...". but unless you want to terminate the complete program, you shouldn't use System.exit
-2

System.exit() Method can be use to terminate program so when you use this method it will terminate program as it reached to parameter value of System.exit() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.