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?
5 Answers
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.
Comments
The usual way that everyone does this -- so far I've never seen a JAR do something different -- is to put class org.foobar.MyClass in the JAR file at the JAR's /org/foobar/ directory. I can't imagine a good reason for doing something differently, as it would impede normal use of this JAR by anyone not doing unusual things.
Comments
The name in the jar file has to match the class name. Unless of course you write a custom class loader to do weird stuff.
You can specify a "directory" within jar file using a jar URL. However, I don't think URLClassLoader handles this correctly (not tried).
A feature that does exist as standard is to add other jar files on to the classpath by specifying them in the manifest.