Package dependent Java JAR files into the project JAR file

When you execute your Java JAR file, you need to include all dependent JAR files in the “fat jar” file, so the Java virtual machine can find them at runtime.

Set up NetBeans to include the JAR files from the library

In NetBeans you need to add the list of dependent libraries to the build.xml file.

  1. On the Files tab double-click the build.xml file to open it in the editor,
  2. Add the lines below to the build.xml file before the </project> statement. Change the value of the second line to specify the name of your project.
    <target name="package-for-store" depends="jar">
        <property name="store.jar.name" value="MY_PROJECT"/>
        <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>
  3. Save the build.xml file.

Build the project

  1. Right click the build.xml file,
  2. Select Run Target > Other Targets > package-for-store
  3. The generated file is going to be located in the store directory.

Leave a Reply