0

I have a fairly simple Java executable jar that runs the main method, checks the availability of some services, and returns (i.e. terminates) normally if the services are up.

The executable jar is run repeatedly as monitor, and upon failure, an alarm is raised in an external alarm and reporting system.

If the services are down, the code throws a RuntimeException and on the command line, the java command throws the exception.

Does this count as a POSIX fail (i.e. return code 1)? Or, do I need to catch the exceptions and explicitly return 1?

Thanks

Steven Solomon
  • 301
  • 1
  • 3
  • 14

2 Answers2

1

I don't think that it's guaranteed to return 1, and I'm not even certain it's guaranteed to return a non-zero value. I think your best bet is to catch that Exception and call System.exit(1).

It's not exactly your question, but they seem to have a better grasp of this than I do if you want to read more on those two diffirent approaches: System.exit(num) or throw a RuntimeException from main?

Community
  • 1
  • 1
Lord Farquaad
  • 712
  • 1
  • 12
  • 33
1

Yes it does. By testing the following code I was able to verify this.

TestDeath.java

public class TestDeath {
    public static void main(String[] args) {
        throw new RuntimeException("I'm dying");
    }
}

JavaProcess.java

public class JavaProcess {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder("java", "TestDeath");

        Process process = builder.start();
        int exitValue = process.waitFor();
        System.out.println("Exit Code: " + exitValue);
    }
}

JavaProcess runs the TestDeath class in a separate process and prints the exit code.

Following is the output.

Exit Code: 1

I had compiled the first class by myself and put it in the current directory of the JavaProcess.

Platform

Windows 7 64 bit
Java 1.8.0_91-b14
11thdimension
  • 10,333
  • 4
  • 33
  • 71