How tor eturn exit code from Java to a shell script

Discussion in 'Java' started by Mark McFarlane, May 8, 2006.

  1. Since main is defined as

    static void main()

    How do I return a value to a shell script so I can check the 'tatus' of
    the execution of a Java program from within the shell script?

    Thanks,

    Mark
    Mark McFarlane, May 8, 2006
    #1
    1. Advertising

  2. I meant to type 'status', not 'tatus'
    Mark McFarlane, May 8, 2006
    #2
    1. Advertising

  3. Thomas Weidenfeller, May 8, 2006
    #3
  4. Mark McFarlane

    Ed Kirwan Guest

    Mark McFarlane wrote:
    > I meant to type 'status', not 'tatus'
    >


    You can return values via System.exit(). For example:

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

    In DOS, you'll see the result in ERRORLEVEL:
    java Test
    echo %ERRORLEVEL%



    --
    www.EdmundKirwan.com - Home of The Fractal Class Composition.

    Download Fractality, free Java code analyzer:
    www.EdmundKirwan.com/servlet/fractal/frac-page130.html
    Ed Kirwan, May 8, 2006
    #4
  5. Thanks Thomas and Ed,

    Telling the JVM to shutdown by calling System.Exit() seemed a little
    extreme to me (I don't know if there would be any undesirable side
    effects of this call), but if it is the conventional practice and the
    only option that's OK.

    Mark
    Mark McFarlane, May 8, 2006
    #5
  6. "Mark McFarlane" <> wrote in
    news::

    > extreme to me (I don't know if there would be any undesirable side
    > effects of this call), but if it is the conventional practice and the


    It is simply a matter of exiting the program that you have written.
    See the API (
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#exit(int) )
    for more info :)

    --
    Martin Gerner
    Martin Gerner, May 8, 2006
    #6
  7. Mark McFarlane

    James McGill Guest

    On Mon, 2006-05-08 at 16:10 +0000, Martin Gerner wrote:
    >
    > It is simply a matter of exiting the program that you have written.


    Well, it's true that it's the normal way to exit a single-function
    "main" style program, but System.exit does more than just exit your
    program -- it also shuts down the vm, takes down running threads, may or
    may not run finally{}'s, etc. It is a pretty harsh way to exit a
    program, more the equivalent of an OS shutdown than a function exit.

    On the other hand, to do any better you have to do some form of IPC.
    I realize the OP is doing something relatively trivial with a program
    that runs in main() and so is nervous about using exit(). It's
    perfectly reasonable and common to exit main with System.exit(), but
    that doesn't stop it from being a nasty, ugly thing to do. It's also
    the only realistic way to get a shell exit code for a utility program.
    James McGill, May 8, 2006
    #7
  8. Mark McFarlane

    Roedy Green Guest

    On 8 May 2006 02:28:33 -0700, "Mark McFarlane"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >static void main()
    >
    >How do I return a value to a shell script so I can check the 'tatus' of
    >the execution of a Java program from within the shell script?

    it is actually:

    public static void main (String[] args)

    and that all matters.

    use the exit( int ) method.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, May 8, 2006
    #8
  9. Mark McFarlane

    Joined:
    Jun 22, 2010
    Messages:
    1
    Are you really just asking for "How do I return an error code from Java to the OS for use in a script?" If that's the case, then simply throwing a throwing an exception from the main method will result in the JVM returning a value of 1. This would be preferential to using System.exit(int) in such a case since any finally blocks will still get executed. On a Unix/Linux box, the shell variable $? will have a value of 1 if an exception is thrown by your main method and 0 for successful program completion.

    Hope that helps.
    , Jun 22, 2010
    #9
  10. Mark McFarlane

    javamazon

    Joined:
    Dec 20, 2011
    Messages:
    1
    simple usage of system.exit(0) which i have tried and its working abosolutly fine.

    try{
    System.out.println(" ** Java Program to Except Name and Age through Console ** ");
    System.out.println(" ** Enter Your Name and Age **");
    Scanner in = new Scanner(System.in);
    name=in.nextLine();
    age=in.nextInt();
    in.close();
    System.out.println("Your Name - "+name);
    System.out.println("and your Age - "+age);
    }
    catch (Exception e)
    {
    System.out.println(" Error : "+e);
    }
    finally
    {
    System.exit(0);
    }
    }

    source : http://javamazon.com/2011/12/20/how-to-read-input-from-console-keyboard-in-java/
    javamazon, Dec 20, 2011
    #10
    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. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,880
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,795
    sandeep1976
    Nov 8, 2006
  3. Yujo
    Replies:
    2
    Views:
    396
    Gabriel Genellina
    Apr 10, 2007
  4. kaleem
    Replies:
    8
    Views:
    788
    Tad McClellan
    Dec 14, 2006
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    241
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page