Skip to content
September 15, 2009 / Narayan

Merging JAR files to single JAR via IDE NetBeans 6.7

Actually I am going to  show the simple way to merge many jar files to a single jar file .

Requirements:

  • Firstly We need the NetBeans6.x.x
  • JDK 1.6

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

I ‘m using NetBeans 6.7.1 and now i am starting my demonstration.

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
.

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 single jar file  via XML . Now copy and paste the following text to the end, just before the final line, which is the closing tag</project>

<target name="package-for-store" depends="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="MarsRoverViewer"/>

      <!-- 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>

You must see the code like this:

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

Ok i’ll explain in brief: On the above code there you will find the line like this:

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.

-Source (http://ngopal.com.np)

Advertisements

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: