Deliver Your Java Application in One-JAR™! Copyright 2004-2007 by P. Simon Tuffs, All Rights Reserved.

Opening the JAR

One-JAR uses a classloader which knows how to load classes and resources from Jar files inside a Jar file. To help provide some structure to the classloading process, the One-JAR JarClassLoader looks for a main program inside a main directory in the Jar file, and looks for supporting Jar files inside a lib directory. Here is what a candidate Jar file would look like set up to run under One-JAR:
| | | Main-Class: com.simontuffs.onejar.Boot
| | | One-Jar-Main-Class: com.mydomain.mypackage.Main
| /main
| | main.jar
| | | com.mydomain.mypackage.Main.class
| /lib
| | commons-logging.jar
| /com.simontuffs.onejar
| | Boot.class
| | etc.
That's pretty much all there is to it. Wrap the main class in a file called main.jar, put it in a JAR folder called main, likewise put supporting Jar files under a folder called lib, change the top-level Main-Class to point to the bootstrap classloader from the One-JAR package, and then point to main class with a new attributes One-Jar-Main-Class. Don't worry, there's an Ant task that will do this, which I'll get to in a moment.

But first try running an application build with One-JAR here: one-jar-example-0.96.jar. Download this application and run it using java -jar one-jar-example-0.96.jar. Then try it with verbose mode: java -Done-jar.verbose=true -jar one-jar-example-0.96.jar to get a detailed look at what's going on in the classloader.

The demo will produce an output similar to this:

$ java -jar one-jar-example-0.96.jar
Main: com.simontuffs.onejar.example.main.Main.main()
Test: loaded by com.simontuffs.onejar.DetectClassLoader@7a84e4
Test: codesource is jar:file:/C:/work/eclipse-workspaces/workspace-simontuffs/one-jar/dist/...
Test: java.class.path=one-jar-example-0.96.jar
Util: loaded by com.simontuffs.onejar.DetectClassLoader@7a84e4
... etc.