wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Running a .jar application from a unix shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Running a .jar application from a unix shell script" Watch "Running a .jar application from a unix shell script" New topic
Author

Running a .jar application from a unix shell script

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I have a unix shell script that calls a java application (.jar file).
When the jar file is executed, some analysis is done and either 0 or 1 is printed. I want this value to be returned to the shell script instead of printing on the console, say for eg., shell script can be as follows

===================================================
echo "Running a sample shellscript";
c=`java -jar sample.jar`
< some operation carried out with $c >
===================================================
Since the main method in java is void type, value cannot be returned to the calling shell script. How will I retrieve the value from the jar application? Please let me know how can this be achieved.

Thanks in advance!!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

java.lang.System.exit(exitCode);

This will immediately terminate the application and return the indicated exit code. So normally you'd make this the last statement in the main() method.

To use the exit code, reference the shell variable "$?".
[ December 24, 2008: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


===================================================
echo "Running a sample shellscript";
c=`java -jar sample.jar`
< some operation carried out with $c >
===================================================


To run it in that fashion, just put a "System.out.println (value);" in your Javafile, especially useful, if there are different results indicating success possible, because exit-values != 0 normally indicate failure.

Instead of
, which is discouraged, use

because it is better readable, and better cascaded.


http://home.arcor.de/hirnstrom/bewerbung
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi,

Thanks for your reply!!! It is working for me now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a .jar application from a unix shell script
 
Similar Threads
Need help for Shell Script to reduce a file
Tomcat console in UNIX
How return a string from java program to the shell which invokes thee java command
Return value from main
Unix shell script - oracle function