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.
- On the Files tab double-click the build.xml file to open it in the editor,
- 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>
- Save the build.xml file.
Build the project
- Right click the build.xml file,
- Select Run Target > Other Targets > package-for-store
- The generated file is going to be located in the store directory.