java and status codes.

Discussion in 'Java' started by horos11, Jun 10, 2009.

  1. horos11

    horos11 Guest

    All,

    Much to my dismay, it doesn't look like the exit code with java
    (System.exit()) is hooked up with the OS. ie - if I say:

    public class samp
    {
    public static void main(String[] args)
    {
    System.exit(256);
    }
    }

    compile and run this as in:

    javac samp.java
    java samp

    and then check the error status from the shell, I get:

    echo $? // prints '0'

    why is this? Is there a way to get the sun JVM to set $? on failure?
    Else, how do you know the program failed, from a shell standpoint?

    Ed
    horos11, Jun 10, 2009
    #1
    1. Advertising

  2. Steven Simpson wrote:
    > horos11 wrote:
    >> public class samp
    >> {
    >> public static void main(String[] args)
    >> {
    >> System.exit(256);
    >> }
    >> }
    >>
    >> [...] and then check the error status from the shell, I get:
    >>
    >> echo $? // prints '0'

    >
    > Try some other values. For exit(x), I seem to be getting (x & 0xff).


    That makes sense.

    It is obviously a wrapper around the stdlib exit call.

    And the C standard says:

    If the value of status is zero or
    EXIT_SUCCESS, an implementation-defined form of the status successful
    termination is
    returned. If the value of status is EXIT_FAILURE, an
    implementation-defined form
    of the status unsuccessful termination is returned. Otherwise the status
    returned is
    implementation-defined.

    And the Unix specification say:

    The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, [CX] [Option
    Start] or any other value, though only the least significant 8 bits
    (that is, status & 0377) shall be available to a waiting parent process.

    So assuming it is on Unix then this makes sense.

    Arne
    Arne Vajhøj, Jun 11, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg  --
    Replies:
    4
    Views:
    2,131
  2. JThomas
    Replies:
    2
    Views:
    4,456
    JThomas
    Jan 24, 2006
  3. Replies:
    2
    Views:
    2,794
    Malcolm
    Aug 20, 2005
  4. Allen
    Replies:
    1
    Views:
    625
    Mark Rae [MVP]
    Dec 3, 2007
  5. horos11

    java and status codes.

    horos11, Jun 10, 2009, in forum: Java
    Replies:
    3
    Views:
    619
    Tom Anderson
    Jun 11, 2009
Loading...

Share This Page