This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Return value from main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Return value from main" Watch "Return value from main" New topic

Return value from main

Rajendra Gangarde

Joined: Sep 10, 2004
Posts: 7
I am executing java application from shell script. I want to return some value from application which will be used as status code by shell script.
How we should be able to do it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

Use System.exit(returncode) to end the application (see the API documentation).
[ September 06, 2005: Message edited by: Jesper de Jong ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Jesper de Jong:
Use System.exit(returncode) to end the application (see the API documentation).

[ September 06, 2005: Message edited by: Jesper de Jong ]

Using System.exit(n) is probably the best way to return an error code, in a simple Java application.

However, in a complicated system, it is less than ideal. System.exit() kills the whole Java process. It does not wait for non-daemon threads to finish, so important tasks might get terminated at a bad time.

Further, if your code might get incorporated into some larger overall application framework, then System.exit() is a bit of a disaster because it will terminate the whole application framework, not just your code.

Using the standard Java launcher (e.g. java.exe on Windows), there is no way to control the process exit code, except System.exit(). However, if you write or obtain a different launcher, alternative facilities may be added. For example, I have a launcher with a native method setExitCode(), through which I can set the code that the Java process will use, when it finally exits. Writing launchers Google for "Java Invocation Interface".

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79

I think 1 option is to store the return code in a file and retrieve that in the shell script
subject: Return value from main
Similar Threads
return a value from shell script to java program
How to return a value from java to script
how to call shell script(interactive) from java code
Unix shell script - oracle function
Running a .jar application from a unix shell script