How to invoke from UNIX script and pass back return code?

Though I am an experienced developer, I am new to java. I created a class containing a single method. I have performed my testing by running the class class.method from the command line in a UNIX (Solaris) environment. Now, I would like to have the class.method invoked from a UNIX shell script, and to return a success/failure indicator from the method, to the UNIX script. I modified the method to make it return char, rather than being defined as void. Within the class and method, I declared and initialized a char variable. I added a finally clause which contains a single return statement, returning the char return code variable. Within my UNIX script, I invoke the class/method as follows:

return_code = java myClass

This does not seem to be invoking the method however. Can someone please tell me what I am doing wrong? Or is more information needed in order for someone to help me out.

Please let me know.

Thanks.

Brad

[968 byte] By [Brad1a] at [2007-10-2 20:09:03]
# 1
Try instead to do something like:System.exit(retVal);where retVal is the "return" value of your code. The shell script will then see the exit value correctly.
stdunbara at 2007-7-13 22:49:32 > top of Java-index,Java Essentials,New To Java...
# 2
And also, the signature of the method that you execute via "java classname" must bepublic static void main(String[] args)
DrClapa at 2007-7-13 22:49:32 > top of Java-index,Java Essentials,New To Java...
# 3

stdunbar,

Using your suggestion of System.exit(retVal); seems to allow the java method to be performed successfully (Thank You). But I am still having a problem with the value being recognized by the shell script.

In my script, I'm doing the following:

java MyClass inputparameter > return_code

export return_code

echo $return_code

But return_code does not seem to contain a value. Just before the System.exit(retVal); I added System.out.println("return code = " + retVal ); and I can see that retVal. When I run the java method outside of the UNIX shell script, I can see that retVal does indeed contain a value. So I think my problem might actually be the code in the UNIX script.

Thanks again.

Brad

Brad1a at 2007-7-13 22:49:32 > top of Java-index,Java Essentials,New To Java...
# 4
In the UNIX shell, the variable $? holds the "exit status" of the previous command line.Try this:java MyClass inputparameterreturn_code=$?echo $return_code
ValentineSmitha at 2007-7-13 22:49:32 > top of Java-index,Java Essentials,New To Java...
# 5
Got it to work.Thank You Everyone for your assistance ! ! !(I think I've found a new favorite site)
Brad1a at 2007-7-13 22:49:32 > top of Java-index,Java Essentials,New To Java...