Forums Register Login

exit codes from Java programs

+Pie Number of slices to send: Send
[ Jim Yingst here. This topic was split off from this thread in the Beginner forum. The original post has little to do with the thread below, except that it mentioned the possibility that a main() method might have a return code. Which it doesn't, in Java, but in C it does. - Jim ]
[ September 06, 2007: Message edited by: Jim Yingst ]
+Pie Number of slices to send: Send
I'd never really thought about it before, but I am wondering whether multi-threading is the reason for the difference between C and Java.

C was designed when single-threading was the norm. The assumption was that the program ended when the main thread ended. Therefore, using the return value of main() as the process exit code made sense.

For Java, many programs have a different life-time to the main thread. Often, the main thread kicks off loads of threads for various functions, then the main thread itself exits, leaving those threads running in the process. Therefore, it makes no sense that the return value of main() should be the process exit code.

I think it's a shame that the only way to set an exit code in a Java program is System.exit(int). Using System.exit() is evil and should not be encouraged; it ruins the reusability of your code and makes it unsuitable for embedding in any over-arching application manager. I think they should have had a System.setExitCode() method, or something like that.

Any thoughts, anyone?
+Pie Number of slices to send: Send
You are probably correct about the "int" return value in C.

Isn't the reason for an "exit value" in C that the exit value is used to check for errors in a program which has just finished running? In Java errors are always (we hope) reflected by an Exception (or Throwable) being thrown. So ought we to use the exit value at all? I know some IDEs (is it NetBeans) say "Application has exited with a value of 0," so you could use different exit values to find which System.exit() has been called, but is the exit value really of any use in Java at all?

[edit]-a couple of grammatical corrections][/edit]

[ September 06, 2007: Message edited by: Campbell Ritchie ]
[ September 06, 2007: Message edited by: Jim Yingst ]
+Pie Number of slices to send: Send
[Campbell]: Isn't the reason for an "exit value" in C that the exit value is used to check for errors in a program which has just finished running? In Java errors are always (we hope) reflected by an Exception (or Throwable) being thrown. So ought we to use the exit value at all? I know some IDEs (is it NetBeans) say "Application has exited with a value of 0," so you could use different exit values to find which System.exit() has been called, but is the exit value really of any use in Java at all?

Errors and Exceptions are greatly preferred within a Java program, true. But what if you're writing a Unix shell script to run the Java program as part of a larger series of actions, and you need to know if the program had any errors or not? You could capture the error output and parse it I suppose. But in general it would be a lot easier to use a numeric error code here.

What the number means is really something to work out between the script writer and the Java program author, though a standard simple convention would be 0 = success, anything else = failure of some sort. In some cases you might want to change this so that nonzero results can indicate something more useful, e.g. number of records successfully processed (and a negative number indicates errors). This amount of info you can pass this way is very limited, but it's a fairly simple way to communicate between a process and the script that spawned it.

In general I'd say it's pretty uncommon to see exit codes used in Java programs, but it is possible. I think it seemed like a natural thing to allow for, given that Java was created by a bunch of heavy Unix users.
+Pie Number of slices to send: Send
Thank you. You were right, it did make for interesting conversation once removed from Beginners.
+Pie Number of slices to send: Send
Windows supports exit codes in scripts, too. And Mac OS X does, because it's really Unix in a Sunday dress.
+Pie Number of slices to send: Send
I figured Mac was pretty much included when I said Unix. I wasn't sure about Windows, since I just use Cygwin to pretend I'm using Unix. Thanks for the additional info.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 16698 times.
Similar Threads
HELP!! People who passed or submitted - DB interface
Using real words (Was: Finding Nth Largest element of an array without sorting
wait & notify
Logical operator precedence
Compile time constant ?
small swapping problem please please reply!!!!!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:16:14.