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?
I believe you can accomplish this by using the MANIFEST file. If I remember correctly, you can specify the location of a particular file within the jar.
Why would you do the latter? It's easier to just put it in the correct directory based on the package name, which avoids having to edit the MANIFEST file or doing any other sort of special settings.
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.
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.
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.