• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exit codes from Java programs

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[ 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You were right, it did make for interesting conversation once removed from Beginners.
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Windows supports exit codes in scripts, too. And Mac OS X does, because it's really Unix in a Sunday dress.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic