Sabira Sajjad
Sabira Sajjad

Reputation: 43

How to execute a java class in jar from a shell script

I have a jar file with 2 classes class A and class B and each of them has a main method.

I want to create two separate shell scripts to call each one of them. Script 1 for class A and script 2 for class B. In each script I need to do the following:

  1. Call the particular class and pass arguments to its main method args[0]
  2. The passed arguments should be accessible using System.getProperty()
  3. The main method should be able to send back appropriate message to the shell script to display it on the console.

I read on StackOverflow that this can be done by writing the string to stdout with: System.out.println("My message");

My question is how should the shell script command be written for the above java class call?

Currently I was playing around with this and it's not working:

output = $JAVA -classpath "$CLASS_PATH" -jar /location_of_jar/myjar.jar packagenameofmyclass.MyClass -Dusr.dir=$CUR_DIR $input1 $input2 RC=$?
echo "$output"

$JAVA CLASS_PATH CUR_DIR is a variable whose value I have set above in the script file.

Please could someone help me out? Am I even going in the right direction.

Upvotes: 1

Views: 9270

Answers (1)

Spike Williams
Spike Williams

Reputation: 37295

Try:

output=`$JAVA -classpath "$CLASS_PATH" -jar /location_of_jar/myjar.jar packagenameofmyclass.MyClass -Dusr.dir=$CUR_DIR $input1 $input2`

The ` marks should make it execute.

Upvotes: 1

Related Questions