Three classes with full source code and documentation that allows to extract resources from a file, from inside a JAR file that is currently executing (java -jar foo.jar), from a JAR that is included in the aforementioned executing JAR and to define a classLoader that can work with JARs, even if those JARs are JARred inside the currently executing JAR.
If you use it in any of your projects I'd like to hear from you. If the project is commercial, please ask before using the classes.
Everything should be transparent to the user, so if you test the application without compacting it with 'jar', it should work, and it should work even when it will be jarred, or partially jarred etc..
To see a concrete example of how it works, see the MiniInstaller.
The typical layout is:
Foo.jar +- MyClass +- BootstrapJarClassLoader +- BUNDLE.jar | +- Test.class | +- ResourceMgr.class | +- DATA/ | +- girl.jpg +- JARS/ +- SUB.jarIn this case, when running "java -jar FOO.jar" MyClass, using BoostrapJarClassLoader loads classes from BUNDLE.jar and executes them. Besides classes inside BUNDLE.jar can access files that resides in their JAR (which is JAR-red inside Foo.jar) via ResourceMgr. The trick can go on further on and Test.class can load classes or execute the Jar named SUB.jar!!
What you have in the ZIP
test for MetaJarUtilities
simply run maketest.bat (in unix maketest.sh) and then copy "supermetatest.jar" in an empty directory and run it.
you can experiment with "metatest.jar" or the various .class file to see how the behaviour is the same (and also the code you have to write) regardless of where
For the moment being a JAR structured like this works:
FOO.jar +- SuperMetaJarTest.class (uses BootstrapJarClassLoader.class) +- net/ | +- yagga/ | +- util/ | +- BootstrapJarClassLoader.class | +- metatest.jar (content below) | +- net/ | | +- yagga/ | | +- util/ | | +- Meta Jar Utilities classes here... | +- jars/ | | +- test.jar | +- TestmetaJar.class | +- text.txt | +- jars2/ +- test2.jar
It works because SuperMetaJarTest calls and executes the jar "metatest.jar" via a BoostrapJarClassLoader (a convenience class that encloses all the MetaJarUtilities in one class).
This jar looks for a file named "text.txt" (resides in the same jar, while the jar is executed from inside a jar.
It then executes a parallel jar (a jar on the same level of hyerarchy as metatest.jar) i.e. "jars2/test2.jar". This works even if the test suite is not jarred, or if it is partially jarred (only metatest.jar).
Currently it is not possible (but should be no great pain achieving it) calling "test/test.jar" from inside metatest.jar. (this should be a jar inside a jar inside the executing jar.. quite unlikely).
Yagga's Java pages are copyright(C) 1999