domenica 24 maggio 2009

Java: Jar classpath [ENG]

So you have these jars in your c:\tmp\ directory:
main.jar
lib1.jar
lib2.jar
and you issue the command:
c:\tmp\> java -cp lib1.jar;lib2.jar -jar main.jar
Right?
Wrong! All you get is the dreaded java.lang.NoClassDefFound
exception. As I recently found out (much to my chagrin: I'm supposed to be a veteran Java developer) the -jar option is mutually exclusive of -classpath.
Why did Sun made it this way? Why java.exe does not complain and provide help in that case? Beats me. How you work this around? You have to specify the classpath in your MANIFEST.MF file packaged with your jar.
So to make the jar work in the previous example:
java -jar main.jar
and, in your MANIFEST.MF:
Class-Path: lib1.jar lib2.jar

Nessun commento: