Tuesday, September 23, 2008

Classpath is ignored when using java -jar

I have an application that I packaged in a JAR, and I needed to add a directory to the classpath so my logging library Logback could find the configuration file I created (using a wizard). So I figured I could just add it using -cp:
java -cp mydir -jar myjar.jar
But no, the classpath doesn't get set when you use the -jar option (you can check this by calling System.getProperty("java.class.path")). It turns out you have to add it to the manifest file when creating the JAR:
Class-Path: classpath/
Also notice the slash at the end. It's not optional if you want to specify a directory. Don't ask me why, but it will only work if you add the slash.

No comments: