passing java parameters to a unix script
Hi,
I would like to know if it's possible to pass parameters from a java application to a unix script. I have a unix script that launches the application. If fatal errors occur in the application, they are caught and the application exits with System.exit(-1). I would like the unix script to know that the application has stopped and more importantly, if it has stopped in a correct way of if something has gone wrong. Any help would be greatly appreciated! greetz |
As I understand it the whole java system.exit(x) method is designed for just that purpose. The quitting with a number lets a calling script know what has happened after it has called a program/other script.
I know no unix scripting but pseudo-code would look something like this. (result is a variable to hold result of call to app) result = call to application. if result equals 0 application executed OK so do something else if result equals -1 application failed so do something else I think thats how to catch the system.exit() number. A unix scripter may give a proper code solution but thats something to work with. |
I know the general structure, but I need a specific example for unix.
btw, the system.exit returns 0 if everything went ok, any other integer means something went wrong. |
one way you can do it like this, basically use "if !" or if depending on what way your checking
#!/bin/bash if ! java -cp /home/myhome test /home/myhome test; then echo "Error Occured"; fi same as suggested above really |
Tried it and it works now. Thank you very much!
|
Okay, in addition to the post above:
if ! java -cp /home/myhome test /home/myhome test; then echo "Error Occured"; fi works fine. However, is there a possibility to test for the actual return value from the java program? example: the java program can do a System.exit(-1) at one line and a System.exit(-2) at another location. Can I test this in the script? I already tried if java -cp /home/myhome test /home/myhome test = -1; then echo "Error -1 Occured"; fi but that didn't work. Any suggestions? |
Just did a little search and came up with the following. It may not be 100% correct but hopefully it will help you.
if ["`java -cp /home/myhome test /home/myhome test `" = "-1"]; then echo "Error -1 happened"; else if ["`java -cp /home/myhome test /home/myhome test `" = "-2"]; then echo "Error -2 happened"; else echo "No problems there!"; fi Note the ` (back quote - key left of 1). This means run the command and substitute the output (hopefully the system.exit output). Hope this helps, actually I hope it works! Let me know how you get on! |
Thanks for the suggestion Andy@DP, but it didn't work.
I also tried if [ `java -cp /home/myhome test /home/myhome test ` -eq "-1" ]; then echo "Error -1 happened"; fi but I get 'unknown test operator' as error message. |
There is another possibility.
You can check the return value of the program just completed in the shell script by checking the shell variable '$?'. For example , your program return any value between -1 to -9 depending upon the errors, you can check it IMMEDIATELY by checking the variable $?. It holds the value 0 if no value is returned and the program has executed successfully, and other values if the program returns it. To output the value, just use '$ echo $?' |
OK, I think I may have an answer this time.
run the script (dunno how to do this exactly, hopefully you do) java -cp /home/myhome test /home/myhome test; use the special variable $? (returns the exit variable from the last command) and test that for the result. Hopefully this helps! EDIT: just realised that the above post must have been entered as I was typing. Stealing my glory ;) |
I just tried echo $?, and the value it shows is 255.
Of course, this is not the value returned by the program, the program returns either -1 or -2 when it runs into problems. |
Was that with the script working or not, which value should it have returned?
Just noticed you said you typed echo $?, did you type that or $ echo $? :confused: |
echo $? returns the value of the last completed program. So, you have to check for it immediately after you complete the java program. So, if you check it after your script is executed, you have the return value of your script.
Try executing your program, i.e, '$ java progname' and now type '$ echo $?' and you will get the return value. You can also try it with other commands such as ls. Type '$ ls djfnf' and you will get -1 Type '$ jksnfkn' and you will get 255 (I don't remember). So, try it and post the reply. PS.. nephilim, I am happy to see that we both think alike, and also I have beat you in this!!! |
The '$' in '$ echo $?' is just your promt.
|
OK I know that now, just a temp moment of insanity!!
I just ran $ echo $? (obviously wrong!) and then echo $?, it returned 127, should have been -1. So I'm thinking 127 = -1 and 255 = -2, possibly. |
All times are GMT -5. The time now is 09:51 AM. |