• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return value from main

 
Rajendra Gangarde
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use System.exit(returncode) to end the application (see the API documentation).
[ September 06, 2005: Message edited by: Jesper de Jong ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra,

I think 1 option is to store the return code in a file and retrieve that in the shell script
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic