java.lang.NoClassDefFoundError while running jar

孤街醉人 提交于 2019-12-11 13:16:48

问题


I use SLF4J logger. jar file is placed in ../lib directory. Application was created using maven and in Netbeans. But I want to do all by myself to teach myself.

Application is compiled with command (of course there are no spaces after semicolons - I added them for better readability):

javac -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar 
-d target/classes src\main\java\pl\alden\convertcosts\*.java

and I can run it with

java -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar;
  target/classes
  pl/alden/convertcosts/App

I know I can set all jar files as clclasspath system variable (Windows 7).

I want to put all into jar and run it from this jar. To create jar I use

jar cvfm convertcosts.jar manifest.mf -C target/classes/ .

And I have proper jar: jar tf convertcosts.jar gives

META-INF/
META-INF/MANIFEST.MF
.netbeans_automatic_build
log4j.properties
pl/
pl/alden/
pl/alden/convertcosts/
pl/alden/convertcosts/App.class
pl/alden/convertcosts/ConvertCostsException.class
...

Now I want to run application from bat file:

set classpath=../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar
java -jar convertcosts.jar

As a result I have

Exception in thread "main" java.lang.NoClassDefFoundError: 
  org/slf4j/LoggerFactory
  at pl.alden.convertcosts.App.<clinit>(App.java:10)

To me it looks like main jar file does not see library jar files.

What should I change?


回答1:


Better to stick with the explicit command-line classpath definition:

java -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar
  -jar convertcosts.jar

Defining classpath or CLASSPATH environment variable has a system-wide affect and you should be aware that not all java applications need these particular jars on their classpath. If you really must, then at the very least use the full paths instead of ../lib/ when defining the value of the CLASSPATH.



来源:https://stackoverflow.com/questions/22519378/java-lang-noclassdeffounderror-while-running-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!