Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

How tor eturn exit code from Java to a shell script

Thread view: 
Mark McFarlane - 08 May 2006 10:28 GMT
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 - 08 May 2006 10:54 GMT
I meant to type 'status', not 'tatus'
Thomas Weidenfeller - 08 May 2006 11:11 GMT
> I meant to type 'status', not 'tatus'

System.exit(code);

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Ed Kirwan - 08 May 2006 11:21 GMT
> 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%

Signature

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

Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html

Mark McFarlane - 08 May 2006 14:03 GMT
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
Martin Gerner - 08 May 2006 17:10 GMT
> 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 :)

Signature

Martin Gerner

James McGill - 08 May 2006 18:50 GMT
> 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.
Roedy Green - 08 May 2006 19:04 GMT
On 8 May 2006 02:28:33 -0700, "Mark McFarlane"
<markamcfarlane@hotmail.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.