Skip to content
August 14, 2009 / Narayan

Combining JAR files to one JAR from IDE NetBeans

Actually I am gonna demonstrate the simple way to create the jar file which is a combination of multiple jars’ .
Requirements:

  • Firstly We need the NetBeans
  • JDK 1.6

Here we’ll create a sample Project which is included in NetBeans.

I used NetBeans 6.7.1 and now i am starting my demonstration.

(Also you can create a project where there is presence of jar files)

Let’s make a project File>New Project> …. and as your requirement you can make a project but there

is need of some extra jar files . If you are not familiar with adding jar files to Projects then ,

Please refer this:

Now you are able to attach jar files in Project.

It’s time time to have some trick now, just choose Files Tab in Project Window or just press Ctrl+2 as shown in figure

File Tab

File Tab

Now there you will see a build.xml which can be double click to edit it’s source. Now here we can combine our jar files

into one jar via XML . Now copy and paste the following text to the end, just before the final line, which is the closing tag</project>:

&lt;target name=&quot;package-for-store&quot; depends=&quot;jar&quot;&gt;

&lt;!-- Change the value of this property to be the name of your JAR,
minus the .jar extension. It should not have spaces.
&lt;property name=&quot;store.jar.name&quot; value=&quot;MyJarName&quot;/&gt;
--&gt;
&lt;property name=&quot;store.jar.name&quot; value=&quot;MarsRoverViewer&quot;/&gt;

&lt;!-- don't edit below this line --&gt;

&lt;property name=&quot;store.dir&quot; value=&quot;store&quot;/&gt;
&lt;property name=&quot;store.jar&quot; value=&quot;${store.dir}/${store.jar.name}.jar&quot;/&gt;

&lt;echo message=&quot;Packaging ${application.title} into a single JAR at ${store.jar}&quot;/&gt;

&lt;delete dir=&quot;${store.dir}&quot;/&gt;
&lt;mkdir dir=&quot;${store.dir}&quot;/&gt;

&lt;jar destfile=&quot;${store.dir}/temp_final.jar&quot; filesetmanifest=&quot;skip&quot;&gt;
&lt;zipgroupfileset dir=&quot;dist&quot; includes=&quot;*.jar&quot;/&gt;
&lt;zipgroupfileset dir=&quot;dist/lib&quot; includes=&quot;*.jar&quot;/&gt;

&lt;manifest&gt;
&lt;attribute name=&quot;Main-Class&quot; value=&quot;${main.class}&quot;/&gt;
&lt;/manifest&gt;
&lt;/jar&gt;

&lt;zip destfile=&quot;${store.jar}&quot;&gt;
&lt;zipfileset src=&quot;${store.dir}/temp_final.jar&quot;
excludes=&quot;META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA&quot;/&gt;
&lt;/zip&gt;

&lt;delete file=&quot;${store.dir}/temp_final.jar&quot;/&gt;

&lt;/target&gt;

You must see the code like this:

XML Format

XML Format

Ok i’ll explain in brief:

On the above code there you will find the line like this:

&lt;property name=&quot;store.jar.name&quot; value=&quot;MarsRoverViewer&quot;/&gt;

Here you can edit the code according to your requirements

<property name="store.jar.name" value="MarsRoverViewer"/>
  • value=”MarsRoverViewer” defines the filename of the jar which is the combination of all jars.
  • property name=”stor.jar.name” define the property ‘s name which is stored in a directory /store and the with name of MarsRoverViewer which is defined in attribute value .

After all you have finished your coding phase.

Now, When you’re ready to package all of your classes and libraries into a single JAR file, switch back to the Files view in the upper left, then right-click the build.xml file. Then choose Run Target>Other Targets>package-for-store. Now the compiler will start to combine your class files and other libraries to a single JAR file named MarsRoverViewer.

Executing this Ant target will create a directory called store inside your project directory. NetBeans and Ant will then copy all of the classes and libraries into a temporary JAR file and strip out any unwanted files, placing the final result under the JAR name that you specified earlier. NetBeans will then delete the temporary file. The results are shown in Figure

Advertisements

2 Comments

Leave a Comment
  1. lilikindsli / Oct 4 2009 5:50 pm

    AaqdKE I want to say – thank you for this!

    • Narayan / Oct 5 2009 4:54 am

      i’m really appreciated

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: