views:

41

answers:

1

How does the Class-path in the MANIFEST.MF file determine the relative location?

1) Say I have a JAR with a jar inside lib/somejar.jar and ofc the manifest file is inside META-INF/MANIFEST.MF. How would I set the classpath....? Would it be Class-path: lib/somejar.jar or ../lib/somejar.jar ?

2) Let's say the somejar.jar also has other jars inside of it that the main jar depends on. And of course somejar.jar also has its own MANIFEST.MF file with a correctly set Class-path field. Now lets then say that the original main jar file, during run-time, needs to access one of those JAR files, will it be able to do so as long as somejar.jar is on main.jar's classpath? Or are the relative directories messed up since the main jar is running 'somejar.jar' isn't the root anymore?

minor update: This will all run in a web environment, on an application server.

+1  A: 

When doing it "plain vanilla", you can't have a JAR inside a JAR. You need to either put it outside in a known path relative to the main JAR, or extract the JAR's contents in the main JAR.

But since Eclipse 3.5 you can do this with a builtin trick. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.

alt text

When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.

BalusC
@BalusC: That's interesting. Techincally mine would be even more then just a jar in a jar (main.jar -> some.jar -> vendorjars...). Now let's say this is on an application server as opposed to te vanilla example where I can't have a jar inside a JAR... does this mean that the app server is using a class loader to load some.jar and the vendor.jar(s)?
Zombies
No, Eclipse includes the classloader in the JAR itself. The JAR would then exist of Your code + 3rd party JAR(s) + Eclipse code + Eclipse-specific MANIFEST.MF. It's runnable standalone. But if don't want a **runnable** JAR (because you started talking about appservers, I realized you don't want (and can't use) a runnable JAR), then you should extract the JAR contents into the main JAR (so that the original JAR classes ends up in the same folder structure). There is absolutely no way of having JAR-in-JAR as a simple library.
BalusC