aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Return value from main" Watch "Return value from main" New topic
Author

Return value from main

Rajendra Gangarde
Greenhorn

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: 14345
    
  22

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 8 API documentation
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
Rajendra,

I think 1 option is to store the return code in a file and retrieve that in the shell script
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return value from main