Loading....
Recent Article links:

Archive for June, 2008

Making a JAR file including dependencies

Consider the following scenario: one has a Java application (created, for example, with NetBeans) which is distributed as a JAR file. However, the application has some libraries (also distributed as JAR files) it depends on.

However, rather than distributing all JAR files separately, we just want one JAR file containing the complete application. How can we do this? Unfortunatetly, as far as I know, NetBeans has no automatic way to do this, so I wrote a small script, which I placed in the root of the NetBeans project folder (though it by no means depends on using NetBeans).

In my case, I have an executable JAR file called dist/IncassoHelper.jar, which depends on the two libraries lib/inieditor.jar and lib/opencsv-1.8.jar. The following script then makes one IncassoHelper.jar in the root project directory, containing both two libraries:

#!/usr/bin/sh

rm -rf makejar-tmp
mkdir makejar-tmp
cd makejar-tmp
jar xf ../lib/inieditor.jar
jar xf ../lib/opencsv-1.8.jar
jar xf ../dist/IncassoHelper.jar
jar cmf META-INF/MANIFEST.MF ../IncassoHelper.jar *

The script first unpacks all JAR files into the temporary makejar-tmp subdirectory (doing our own file last so that the manifest file from our own JAR is used), and then compiles the JAR file from our manifest and all files.

It’s a bit dirty to have to do this, but as far as I know there is no easier way. Comments are welcome though.