Add jar libraries to the final jar file

When using Netbeans to create Java projects, you might be using extra library files (jar files), for example, twitter4j-core-2.2.3.jar. The default Netbeans packaging excludes these library files when you build the project. This will result in missing class files when you run the jar file. To prevent this we can add the jar files itself as part of the final application. For this do as follows:

1. Add the library jar files into a folder within the project, say /lib

2. When you use the option “Add JAR/Folder” in Netbeans, make sure you use ‘Relative path’ option instead of Absolute path.

3. In the “build.xml” file present in the project folder, add the following:

<target name="-post-jar">

   <jar jarfile="dist/final_dist.jar">

      <zipfileset src="${dist.jar}" excludes="META-INF/*"/>

      <zipfileset src="lib/library1.jar" excludes="META-INF/*"/>

      <zipfileset src="lib/library2.jar" excludes="META-INF/*"/>

      <manifest><attribute name="Main-Class" value="packageName.Main"/></manifest>



Here ‘final_dist’ is the name of the final jar file that includes the library jars.

Make sure to set the value of ‘packageName.Main’ appropriately to the main class file of your main jar file i.e replace ‘packageName’ and ‘Main’ as it is in your project


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s