wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Getting a return code from main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting a return code from main" Watch "Getting a return code from main" New topic
Author

Getting a return code from main

Ezra Exposito
Ranch Hand

Joined: Apr 26, 2001
Posts: 34
Is there a way to get/save a return code
from a java program? I need to integrate it
with ESP, a scheduling program that runs on
the mainframe. I need to launch the
java program from within a Unix shell script
and get some type of return code to determine
if I should do other processing.
Any help would be greatly appreciated ...
prabhat kumar
Ranch Hand

Joined: Apr 11, 2001
Posts: 114
you write public static void main(){}
if you want to return a value write
public static "return type ie. int, String" main(){}
Ezra Exposito
Ranch Hand

Joined: Apr 26, 2001
Posts: 34
I tried doing:

and got a java.lang.NoSuchMethodError exception.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

This is not a Servlet question.

Moved from Servlets -> Java In General (Beginner)
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Main has to return void. To exit with a non-zero status, use:
System.exit(int status);
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You must use
for the main method.
To pass a return code to a UNIX shell do
System.exit(returnCode); as Dave said.
You should then be able to pick it up in a shell using the symbol $? "dollarquestionmark"
-Barry
[ October 25, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
This is one question which I always like to ask. I tried with following code but didnt get anything on my console. How can I catch this integer from here to another program. can somebody help to give some example.

regards,
Arun
bharat nagpal
Ranch Hand

Joined: Oct 26, 2002
Posts: 76
yeah . that could be a way .... using System.exit
as I dont know much about shell scripting... so I thought there is another solution
...if the main methods executes successfully ,its last line will also execute ... in the last line of your main method , perform some task (like renaming a file), then you can check in UNIX ... it may sound funny , but I guess it will serve your purpose
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Yes bhart, perhaps that could be a way..but is there any other decent way of achieving same. I was wondering how I can send the response back to my another java file which is say servlet or something else or perhaps if I call a java program from VB application.
Any other idea?
Arun
bharat nagpal
Ranch Hand

Joined: Oct 26, 2002
Posts: 76
Arun if you need to call java program from servlets...thats easy if you want to instantiate a class from Servlet....
and if u need to call a java program from VB....for that you should use JAVA-COM bridge , you just have to write the WRAPPERS and you can control the tasks(of java application) from VB.I have done it using JAVAREG...
but for simple purpose ... System.exit is good way, as explained by others. although I've never used this...
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
As we've said, you use System.exit(int) to exit a java program with some "exit status code". Also note that if your program exits because the main thread throws an exception, the VM will exit with a non-zero status. A zero exit status usually means "OK", and non-zero means "BAD".
But how you use this value depends on the environment you are running in.
In Unix, you can get the exit status of the most recent command using the shell variable $?.
If you run the program from Java with Runtime.exec(), then you can find the exit status form Process.exitValue().
I have no idea how you do this if you invoke a program from VB or DOS or etc....
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks bharat1 for giving me a new path called "JAVA-COM bridge" can u give me more light on it..where can i find more literature. BTW, how to write my own Wrapper class is also a issue at my end. I am not an expert. can you please help.
Yes Dave. I am running a program from VB and would like to know the status of exit of this java program. What I am try to find out if there are means available if I go crossplatform. BTW, I am on windows OS not on UNIX.
regards,
Arun
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Cross-platform and VB? Not likely. At least not the way I define cross-platform (to mean a program that can be run on several different kinds of platforms, usually including at least unix and windows and probably mac and maybe others).
Ezra Exposito
Ranch Hand

Joined: Apr 26, 2001
Posts: 34
Thanks for the info everyone. You
have been extremely helpfull as usual.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Dave. I used the wrong term. You're right.
Arun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a return code from main
 
Similar Threads
How to return a value from java to script
Dx Ball Game
Calling Java program from Non-Java code
Need an executable jar to return a value
return int to Oracle