0

这是一个shell脚本:

#!/bin/sh

cd ../../../lib
ls
MY_LIB=`pwd`

for jar in `ls $MY_LIB/*.jar`
do
    CLASSPATH="$CLASSPATH:""$jar" 
done
echo $CLASSPATH

cd ..

#java -jar build/libs/myproject.jar -classpath \"$CLASSPATH\"

运行此脚本将打印:

commons-codec-1.6.jar      httpclient-4.2.1.jar        httpcore-4.2.1.jar  logback-classic-1.0.6.jar  slf4j-api-1.6.0.jar
commons-logging-1.1.1.jar  httpclient-cache-4.2.1.jar  httpmime-4.2.1.jar  logback-core-1.0.6.jar
:/home/user/mygitproject/myproject/lib/commons-codec-1.6.jar:/home/user/mygitproject/myproject/lib/commons-logging-1.1.1.jar:/home/user/mygitproject/myproject/lib/httpclient-4.2.1.jar:/home/user/mygitproject/myproject/lib/httpclient-cache-4.2.1.jar:/home/user/mygitproject/myproject/lib/httpcore-4.2.1.jar:/home/user/mygitproject/myproject/lib/httpmime-4.2.1.jar:/home/user/mygitproject/myproject/lib/logback-classic-1.0.6.jar:/home/user/mygitproject/myproject/lib/logback-core-1.0.6.jar:/home/user/mygitproject/myproject/lib/slf4j-api-1.6.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
    at com.test.MainTest.main(MainTest.java:223)
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.core.joran.spi.JoranException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

为什么我会得到例外?

类路径错了吗?

java -classpath 使用不正确吗?

4

2 回答 2

1

位于logback-core-1.0.6.jar但您不能同时使用JoranException和选项。-jar-classpath

请参阅java - Java 应用程序启动器

-jar
...
使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。

于 2013-02-05T09:50:36.163 回答
0

将以下行添加到您的脚本中,以便您可以调试程序正在查找的类是否在其中一个 jar 中:

for jar in $(cut --output-delimiter=" " -d":" -f2- <<< $CLASSPATH); do
    unzip -l $jar | grep "ch/qos/logback/core/joran/spi/JoranException"
    if [ $? -eq 0 ]; then
        echo "'JoranException' found in $jar" 
    fi
done

# Paste the above code before the following line 
#java -jar build/libs/myproject.jar -classpath \"$CLASSPATH\"

如果您没有收到'JoranException' found消息,那么您错过了一个罐子,如果您多次收到相同的消息,那么您在某处有一个重复的课程。如果它只打印一次,请评论此答案以进一步调试它。

顺便说一句,在 linux 中,您可以将您的设置CLASSPATH为:

CLASSPATH=/home/user/mygitproject/myproject/lib/*.jar

它将扩展到该目录中包含的所有 jar

于 2013-02-05T04:27:04.800 回答