Wouldn’t it be nice if you could just wrap up all your classes into one big executable, and run it? Well the Java2 Runtime Environment has a way of doing this using a JAR launcher. If you bundle your class, and all the supporting classes in a file called myapp.jar, and set the MANIFEST.MF appropriately, you can simply say: java -jar myapp.jar
So what’s the catch?
- You must first unjar all of the supporting classes and flatten them out into a single directory tree before creating the final myapp.jar. For example: what happens if your application depends on some of the web-services JAR files from the Java Web Services Developer Pack? There are many of them and they all contain code which lives in the same packages (javax.*, com.sun.* etc).
2. Consider that after you have expanded all of the code into a single tree, you have lost all trace of any code signatures that might have been present. Why? Because code signatures are located in /META-INF/MANIFEST.MF file, and each jar file will contain one of these manifest files, with different content.
3. Likewise, any resources which have the same name (a distinct possibility given that the jar files themselves may contain flattened out packages) will be in conflict: you can only have one file called log4j.properties to control logging, where you might like to enable/disable logging on a boundary set by the individual jar files.
|
Java™ Application Development on Linux® – Free 599 Page eBook |
Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications: |
||||
Wouldn’t it be nice if you could just bundle the supporting jar-files into your myapp.jar file without expanding them? This is the problem addressed, and solved, by One-JAR.
One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.
To help provide some structure to the class loading 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:
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.
One-JAR provides custom classloader that knows how to load classes and resources from jars inside an archive, instead of from jars in the file system. It discovers dependency jar files based on the internal structure of the archive, there is no custom code required to do this. One-JAR archives can be constructed using Ant or Maven2.
Maven Approach
There is a Maven2 plug-in for One-JAR. It is easy to use for Maven projects. Please consult the documentation here: http://code.google.com/p/onejar-maven-plugin/
Ant Taskdef Approach
Detailed use of the One-JAR Ant Taskdef is discussed here. Note that the one-jar-appgen approach uses the Ant taskdef.
SDK Approach
Use of the (deprecated) SDK is discussed here
Key Features
- Stateless/Install Free: One-JAR can contain everything needed by an application.
- Friendly: BSD license, no impediments to commercial integration
- Easy: One-JAR tools for Ant & Maven: an SDK and an application generator
- Supports: Spring 3.0.2 and Guice 2.0 application frameworks
- Invisible: One-JAR works transparently, intercepting resource and class loading and redirecting them back inside the One-JAR
- Clean delegation model, allows classes (including main) to be at the top-level of the One-JAR, and supports external jars.
- No pollution: dependency jars are not expanded into the file system at runtime
- Fast: One-JAR pre-loads all byte code into memory on startup, making for improved runtime performance.
- Flexible: One-JAR can expand data-files into the file-system, and can be an installer if you want one
- Native: can support Native libraries (does require expansion into a temporary directory)
- Tested: over 140 regression tests with 1500+ lines of test-code in the repository.
References:
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
http://one-jar.sourceforge.net/index.php?page=build-tools&file=ant
http://one-jar.sourceforge.net/index.php?page=build-tools&file=ant-example
http://one-jar.sourceforge.net/index.php?page=build-tools&file=maven
http://one-jar.sourceforge.net/index.php?page=build-tools&file=maven-example
Related articles
- Java – How to Decompile Class files from Jar Files (clean-clouds.com)
- How to Import Existing Java Project in Eclipse (clean-clouds.com)











