Thursday, October 8, 2009

How To: Jars in a jar using Eclipse

I wrote a utility using GeoTools which uses a couple classes to transform geometry from one projection to another.  Projecting geometry uses EPSG definitions stored in the gt-epsg-hsql-2.5.7.jar which does not get bundled into an executable jar; for my utility to work as executable jar I needed to include the gt-epsg-hsql-2.5.7.jar.  

There are several approaches to building a jar that contains jars.  They typically involve using ant to bundle the files into the jar and a special class loader.  Writing the build.xml file and the class loader can be tedious.  Fortunately, the Fat Jar eclipse plugin simplifies the process and includes the One-JAR class loader.  

Installing the plugin can be done using Software Updates:

Click on Add Site

Enter the web site URL

Select the Fat Jar plugin and click on Install

Click Finish

Restart eclipse to finish the installation.

To build your project with external jars included, click on Export

Click on Other and select the Fat Jar Exporter

Select the project

Enter the name of the output jar, select the main class, and check off the One-JAR option to use the One-JAR class loader.

Clicking on next opens a dialog to select the jars to include, the default selects all external jars.  Unlike a typical executable jar, all the imported classes are not included so take the default and click on finish.  The Fat Jar plugin will build the jar with the external jars included, note that the final jar can become quite large.

No comments:

Post a Comment