简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM