Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a .jar application from a unix shell script

 
ram shyam
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17614
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

===================================================
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.
 
ram shyam
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply!!! It is working for me now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic