简体   繁体   中英

How to run jar file on shell script in linux terminal?

I made a java project. The project is....output log message and system.out.println message. just simple. So I changed into a jar file(the name is LinuxSample.jar). and I wrote a shell script to run this jar file. Look at this shell script. (speakee is package name and PrintLinux is main class name)

#!bin/bash
CLASSPATH=/home/tangooc/TANGOOC/test/libs/*
CLASSPATH="${CLASSPATH};/home/tangooc/TANGOOC/test/linux/LinuxSample.jar"
java speakee.PrintLinux

this jar file and this shell script work in Window. but linux didn't work. I don't know why

this is error message.

Could not find or load main class

使用java -jar your_program.jar

Hi Best way to run a java application is to set CLASS_PATH and PATH variable first. If your current jar file depends on external jar files you will face lots of problem. Better set your path variable like below and run the application:-

#!/usr/bin/ksh
export PATH=/usr/java/bin:$PATH
# =/usr/java/bin is your java bin folder
#set environment variable CP with all the jar libraries
CP=/home/flussi/xmlEncoder/encoder.jar 
CP=${CP}:/other/jar/somejar.jar

java -Xmx256M -classpath "$CP" "com.myproj.Example"

I made it I changed the shell script.

CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
LIB_TOTAL=/home/tangooc/TANGOOC/test/libs/*
echo ${LIB_TOTAL}
echo ${CLASSPATH}
java -cp ${LIB_TOTAL}:${CLASSPATH} speakee.PrintLinux

also there is another way.

CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
CLASSPATH=${CLASSPATH}:/home/tangooc/TANGOOC/test/libs/*
echo ${CLASSPATH}
java -cp ${CLASSPATH} speakee.PrintLinux

If the someone like me change the shell script. and check a line, a line, a line...

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