1

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?

koral
  • 2,807
  • 3
  • 37
  • 65
  • I would type in the full path to the library. – Mukus Mar 19 '14 at 22:24
  • According to http://docs.oracle.com/javase/tutorial/essential/environment/paths.html the environment variable is all upper case `CLASSPATH`. Try changing that. I'm not sure if it matters. Also if the original CLASSPATH is not empty, add to it rather than replacing it entirely -- the problem could be that it's failing to load SLF4J because some class that *it* needs is not on the path. – Jerry101 Mar 19 '14 at 22:27
  • @Mukus Typing relational path works well when using `java` and `javac` - is there any difference whrn using `java -jar`? @Jerry101 I changed classpath to CLASSPATH and result is same. – koral Mar 19 '14 at 22:35

1 Answers1

0

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.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52