1

It seems this is one way to have a return status code from your main. but I am wondering where does this int go? I tried it on eclipse, But I do not see it on console. how do I get the status code

public static void main(String[] args){
         int status = 123;
          System.exit(status);
     }
Community
  • 1
  • 1
brain storm
  • 30,124
  • 69
  • 225
  • 393

5 Answers5

5

The exit code is usually returned to a script or program that is running your application, for instance, I wrote the following simple java program

class Test {

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

}

I then compiled it using javac Test.java Next I wrote a simple bash script that would run java Test and then print out the exit code like this

#!/bin/bash

java Test
echo $?

And when i run the bash script the numbers 123 are printed to the screen, as $? is the exit code of the last command that was run.

4

That value is known as the exit status

The exit status or return code of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task.

Here's a little Java test to demonstrate it

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Foo {
    public static void main(String[] args) throws Exception {
        if (args.length != 0)
            System.exit(Integer.parseInt(args[0]));
        ProcessBuilder builder = new ProcessBuilder(Arrays.asList("java", "Foo", "42"));
        Process process = builder.start();
        System.out.println(process .waitFor());     
    }
}

compile and run this program without any arguments. It will print

42

This java program starts a child process. When that process ends, it returns the value that was passed to it in System.exit(?) to its parent process which it then prints out.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • how does `args[0]` get `42`? – brain storm Jan 14 '14 at 03:33
  • @user1988876 Because the `java` process I build with `ProcessBuilder` is given the argument `42`, which becomes the argument at index 0 in the `String[]` that the JVM passes to the `main` method. – Sotirios Delimanolis Jan 14 '14 at 03:34
  • does it take each argument of the `list(java,foo,42)` for three Process? – brain storm Jan 14 '14 at 03:35
  • `Integer.parseInt("foo")` will throw `numberFormatException`..? – brain storm Jan 14 '14 at 03:37
  • @user1988876 No, [here's the javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html). Basically it will execute the following command: `java Foo 42`. – Sotirios Delimanolis Jan 14 '14 at 03:38
  • @user1988876 Not with this program, it's actually `Integer.parseInt("42")` because `args[0]` will have the `String` value `42`. – Sotirios Delimanolis Jan 14 '14 at 03:39
  • I see! thanks. one question though is are you spawning a new Thread here? or there is only one Thread which is `main`?, basically why use `Process`? – brain storm Jan 14 '14 at 03:44
  • @user1988876 There is only the main thread. `ProcessBuilder#start()` actually starts a new OS process. – Sotirios Delimanolis Jan 14 '14 at 03:45
  • and it is that `process` where the `main` is written is exiting. I guess it is the new process that calls system.exit() but the parent one terminates normally..correct? – brain storm Jan 14 '14 at 03:49
  • @user1988876 Sorry if I made this more confusing than it needed to be. You launch the first `java` process by executing `java Foo`. The code will skip the `if` block because you haven't provided any arguments. It will then start a second, child, process by executing `java Foo 42`. This will be a separate `java` process executing with the `Foo` class. Since we have provided an argument, the `if` block will be evaluated and the `System.exit` will kill the process and return the exit code to the parent process, which you get back from `process.waitFor()`. – Sotirios Delimanolis Jan 14 '14 at 03:51
  • @user1988876 The parent process terminates normally. It will have returned 0, by default. You can get that value with everyone else's suggestions. I just wanted to demonstrate parent/child relationship. – Sotirios Delimanolis Jan 14 '14 at 03:51
  • great explanation. Thanks for the example. I now understand how it works. – brain storm Jan 14 '14 at 03:57
3

The exit code (status) is returned to the operating system on termination of the JVM so if you were to run it through terminal/command line you would see if the program terminated abnormally.

Nic Robertson
  • 1,198
  • 9
  • 26
1
C:\JavaTools>javac SystemExit.java

C:\JavaTools>java SystemExit

C:\JavaTools>echo %ERRORLEVEL%
123
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

The exit code will be returned to the OS. Return 0 means the program exits without an error and vice versa.

Return different error code means different causes. The could help the OS(or parent process) find out what's wrong with the program.

Weibo Li
  • 3,565
  • 3
  • 24
  • 36