--> What’s happening behind the scenes? Netbeans performs the following actions to build the final Jar. (Which you can do with a script if you feel so inclined.) Compile all source files to class files Extract all class files from library Jar files Compress all class files (include library files) into new single Jar file" /> --> What’s happening behind the scenes? Netbeans performs the following actions to build the final Jar. (Which you can do with a script if you feel so inclined.) Compile all source files to class files Extract all class files from library Jar files Compress all class files (include library files) into new single Jar file"/> PenguinCode – Embedding Libraries in Java Executables (JAR)

Embedding Libraries in Java Executables (JAR)

Posted on 2010-04-18 15:43 in Blog

I’ve been working on developing a small Java utility for use at my place of work. The utility is used for parsing log files and extracting useful engineering data. Often, this work is performed out in the field literally in the middle of a desert] and most of the time the laptop they are using is handed to them as they are walking out the door to the test range. For this reason, the utility we’ve created has been designed to be as stand-alone as possible, requiring no install.

One of the most requested features is the ability to graph the change of a variable over time. The students on my team identified an open source graphing library that worked perfectly. The problem was we now had to ship two files to the test range, not much, but twice as many as before.

Solution

Using Netbeans we were able to modify the build process to embed the library inside the resulting Jar file. Place the following code in your build.xml file, modifying only the red text to match the name of the final Jar.

<target name="package-for-store" depends="jar">

<!—

http://java.sun.com/developer/technicalArticles/java\_warehouse/single\_jar/

Change the value of this property to be the name of your JAR, minus the .jar extension. It should not have spaces.

<property name="store.jar.name" value="MyJarName"/>

-->

<property name="store.jar.name" value="Bundled_JAR"/>

<!-- don't edit below this line -->

<property name="store.dir" value="store"/>

<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

<delete dir="${store.dir}"/>

<mkdir dir="${store.dir}"/>

<jar destfile="${store.dir}/temp\_final.jar" filesetmanifest="skip">

<zipgroupfileset dir="dist" includes="\*.jar"/>

<zipgroupfileset dir="dist/lib" includes="\*.jar"/>

<manifest>

<attribute name="Main-Class" value="${main.class}"/>

</manifest>

</jar>

<zip destfile="${store.jar}">

<zipfileset src="${store.dir}/temp\_final.jar" excludes="META-INF/\*.SF, META-INF/\*.DSA, META-INF/\*.RSA"/>

</zip>

<delete file="${store.dir}/temp\_final.jar"/>

</target>

What’s happening behind the scenes?

Netbeans performs the following actions to build the final Jar. (Which you can do with a script if you feel so inclined.)

  1. Compile all source files to class files
  2. Extract all class files from library Jar files
  3. Compress all class files (include library files) into new single Jar file