Bundle your application in to a Single Jar file

Posted: October 17, 2006 in Technology

Recently I had a requirement where I needed to bundle my application in to a single jar file. Here single jar file means to bundle everything (including util jars) into a single jar file, so that any one can run that jar file by “java –jar “ option or using other options. Bundling application into single jar file also makes the distribution process easier. Initially I was thinking it’s not a big deal and just setting the classpath in jar’s manifest file would be fine.
Here’s required structure of the jar file
– single.jar
—- util1.jar
—- util2.jar
Then I added a classpath entry in manifest for all utility jars. But that didn’t work. After some googling I came to know that classpath mentioned in the manifest is checked and loaded from file system not from the content of the jar. So the class loader checks for util1.jar and util2.jar inside util directory in the filesystem not inside the jar. So we can’t solve this problem by mentioning the class path in manifest.
As I didn’t have any solution for it, after some google search I found an open source tool “one-jar” which actually describes how to load utility jars inside main jar file. In brief One-JAR uses a custom classloader called JarClassLoader to load information from the single jar file and it has it’s classes to control bootstrap process. If you are interested to know how it works, then here is the link

  1. I did give it a try last year… its pretty neat. I used it for few months on a project then had to let go of it for the following reasons:

    If you are using any 3rd party jars, then you have to keep them outside your application jar (i.e. not within your onejar) – Some licensing issues basicaly.
    I remember using few libraries for reports and scanning documents… had to keep these jars outside the onejar – faced classloading issues.. as these jars make heavy use of Class.forName 🙂


  2. [Trackback] Tramadol dog. Tramadol.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s