简体   繁体   中英

java.lang.NoClassDefFoundError while running jar

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 cl classpath 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?

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 .

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