This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a return code from main

 
Ezra Exposito
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried doing:

and got a java.lang.NoSuchMethodError exception.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a Servlet question.

Moved from Servlets -> Java In General (Beginner)
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main has to return void. To exit with a non-zero status, use:
System.exit(int status);
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info everyone. You
have been extremely helpfull as usual.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave. I used the wrong term. You're right.
Arun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic