Java – Specifying classpath for classes inside the JAR itself

classpathjarjava

If I have a class org.foobar.MyClass and want to put it in a JAR file, do I have to put it in the JAR's /org/foobar/ directory, or can I put it in /bin/org/foobar/ and somehow specify /bin/ as classpath inside the JAR itself?

Best Answer

you can include the Class-Path property in your Manifest, listing the jar files your app depends on. The paths will be considered relative to the location of your executable JAR.

For example if your app.jar contains this in the MANIFEST.MF:

Class-Path: lib1.jar,lib/lib2.jar

Then the JVM will expect to find lib1.jar in the same dir as app.jar and a subdirectory called lib containing lib2.jar.