This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
[ 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 ]
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?
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
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?
-a couple of grammatical corrections][/edit]
[ September 06, 2007: Message edited by: Campbell Ritchie ] [ September 06, 2007: Message edited by: Jim Yingst ]
[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.
"I'm not back." - Bill Harding, Twister
Joined: Oct 13, 2005
Thank you. You were right, it did make for interesting conversation once removed from Beginners.
Joined: Oct 30, 2001
Windows supports exit codes in scripts, too. And Mac OS X does, because it's really Unix in a Sunday dress.
Joined: Jan 30, 2000
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.