1

I've been learning java for some months now and i came through this:

System.exit(value);

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 may do:

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

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

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Bill
  • 321
  • 6
  • 15
  • Related with System.exit() values: http://stackoverflow.com/questions/2434592/difference-in-system-exit0-system-exit-1-system-exit1-in-java – Egl Jun 17 '15 at 17:56
  • Yup, i already checked these! – Bill Jun 17 '15 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. – Colonel Thirty Two Jun 17 '15 at 18:02
  • Thanks! I think i get this code's use. – Bill Jun 17 '15 at 18:05
  • But if i use this code in eclipse and run it no value will be printed , all it does is to terminate the programm and all i need is a "break;" so after all this code **System.out()** is useless. Right? – Bill Jun 17 '15 at 18:07
  • 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. – Durandal Jun 17 '15 at 18:17

6 Answers6

5

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`  
Mureinik
  • 297,002
  • 52
  • 306
  • 350
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
GregH
  • 5,125
  • 8
  • 55
  • 109
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) 
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

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

  • 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 Jun 17 '15 at 17:53
  • @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 –  Jun 17 '15 at 17:55
  • 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 Jun 17 '15 at 18:03
  • @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` –  Jun 17 '15 at 18:11
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.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
-1

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.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Suresh Parmar
  • 815
  • 7
  • 15