简体   繁体   English

如何在Linux终端的Shell脚本上运行jar文件?

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

I made a java project. 我做了一个Java项目。 The project is....output log message and system.out.println message. 该项目是...输出日志消息和system.out.println消息。 just simple. 很简单。 So I changed into a jar file(the name is LinuxSample.jar). 所以我变成了一个jar文件(名称是LinuxSample.jar)。 and I wrote a shell script to run this jar file. 我写了一个shell脚本来运行这个jar文件。 Look at this shell script. 看一下这个shell脚本。 (speakee is package name and PrintLinux is main class name) (发言人是程序包名称,PrintLinux是主类名称)

#!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. 该jar文件和此Shell脚本在Window中工作。 but linux didn't work. 但是linux没用。 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. 嗨,运行Java应用程序的最佳方法是先设置CLASS_PATH和PATH变量。 If your current jar file depends on external jar files you will face lots of problem. 如果您当前的jar文件依赖于外部jar文件,您将面临很多问题。 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. 我修改了shell脚本。

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. 如果像我这样的人更改shell脚本。 and check a line, a line, a line... 然后检查一条线,一条线,一条线...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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