javaexitreturn-code

How do I use System.exit() in Java


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?


Solution

  • 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`