Any time a Perl, Python, C++ or Tcl program halts with an unhandled exception, those language runtimes take care to register a non-zero exit code for the process. Even Eclipse-based programs return 1 if they fail during startup. Programs run by the standard java.exe, however, happily return zero no matter how abruptly they end, unless the program calls System.exit() with an exit value. Even AssertionFailedError or UnsatisfiedLinkError are reported back to the calling program as successful exits.

Of course not all systems have program return codes, but Unix and Windows were important enough to warrant java.lang.Process.exitValue() for child processes; don't they also warrant honouring conventions for parent processes?

Is this a flaw in the language design or just in the implementation? Is there an argument that it's a good idea?

  • Wonder if this is not better suited on StackOverflow... Just an opinion, though. – gablin Nov 22 '10 at 12:28
  • @gablin Mmm, indeed I started writing this question on SO then changed my mind and posted it here. – James Nov 22 '10 at 19:16
  • 4
    The java command in Java 6 and Java 7 both seem to respond with a non-zero exit code if an uncaught exception is raised. – dimo414 Sep 8 '15 at 19:39

If the Java Language Specification does not explicitly define what exit value to expect, then it is undefined and you should not rely on it.

You need to catch Throwable in your main method, and call System.exit(1) yourself.

| improve this answer | |
  • 8
    So isn't it a shortcoming to leave it undefined? – James Nov 22 '10 at 19:11
  • 3
    Maybe, maybe not. The only important thing is what the JLS says. – user1249 Nov 22 '10 at 19:41
  • The JLS isn't the only relevant contract, though it is the dominant one. – Deduplicator Feb 13 at 12:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.