Java Application in One JAR


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?

  1. 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

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

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:

OneJar Structure

OneJar Structure

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

About these ads

How to install Ant?


Installing ANT

•             Download Ant binary distribution from:

                Download

•             Set ANT_HOME to where you installed Ant

•             Include $ANT_HOME/bin in PATH

•             Make sure JAVA_HOME is set to point to JDK

After successful installation of Ant, run ant –version command to verify it.

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Built-in Tasks in Ant


 

Java™ Application Development on Linux® – Free 599 Page eBook

Enterprise Java Virtualization:

Understanding the TCO Implications

InfoWorld’s Java IDE Comparison Strategy Guide:

Java Essential Training

Apache Jakarta Commons: Reusable Java™ Components

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications:

Built-in Tasks

File Tasks

<copy>

<concat>

<delete>  

<get>

<mkdir>

<move>

<replace>

Compile Tasks

<javac>

Compiles the specified source file(s) within the running (Ant) VM, or in another VM if the fork attribute is specified.

<jspc>

                Runs the JSP compiler. The task can be used to compile the generated Java source.

<rmic>

                Runs the rmic compiler

Archive Tasks

<zip>, <unzip>

                Creates a zipfile.

<jar>, <unjar>

                Jars a set of files.

<war>, <unwar>

                An extension of the Jar task with special treatment web archive dirs

<ear>

                An extension of the Jar task with special treatment enterprise archive dirs.

Testing Tasks

<junit>

                Runs tests from the JUnit testing framework.

<junitreport>

Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to provide a browsable report of the testcases results.

Property Tasks

<dirname>

                Sets a property to the value excluding the last path element.

<loadfile>

                Loads a file into a property.

<propertyfile>

                Creates or modifies property files.

Misc. Tasks

<echo>

                Echoes text to System.out or to a file.

<javadoc>

                Generates code documentation using the javadoc tool.

<sql>

Executes a series of SQL statements via JDBC to a database. Statements can either be read in from a text file using the src attribute, or from between the enclosing SQL tags.

Refer to Core Tasks for more details.

Single Sign-On for Java and Web Applications

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Transforming a Generic Java IDE to Your Application Specific IDE:

The Java Virtual Appliance—No OS Required

BEA WebLogic® Operations Control: Application Virtualization for Enterprise Java

Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications: