Use NetBeans IDE 6.7 to Combine JAR Files Into a Single JAR File

 
By Robert Eckstein, August 2009  

Articles Index

The Java Warehouse is the repository for Java and JavaFX applications submitted by developers for distribution through the Java Store to hundreds of millions of desktops worldwide. The Java Warehouse Developer Center Release Notes make clear that there is currently no way to upload applications composed of several Java Archive (JAR) files into the Java Warehouse. However, there is a workaround: Just concatenate each of the user and library classes into a single JAR file. This is very easy to do for both Java and JavaFX projects using NetBeans IDE 6.7.1.

To demonstrate how to do this, let's use one of the sample Java projects that is included in the NetBeans IDE 6.7.1 distribution: the Mars Rover Viewer. Open the NetBeans IDE, select File > New Project, then scroll down to the Samples folder and choose Java from the Categories pane. From there, select Mars Rover Viewer in the Projects pane and click Next, as shown in Figure 1. Next, choose a suitable location for the project and click Finish.

Creating the Mars Rover Viewer Sample Project in NetBeans
Figure 1 - Creating the Mars Rover Viewer Sample Project in NetBeans.
 

We've chosen this project because it makes use of a popular compile-time library that is not included with the standard Java Development Kit (JDK): the Swing Application Framework. To verify that this library is being used, right-click on the project in the upper left pane and choose Properties. From there, select Libraries on the left pane to verify that the Swing Application Framework is indeed listed, as shown in Figure 2. Then click OK to close the Project Properties dialog box.

The Mars Rover Viewer Application Uses the Swing Application Framework Library
Figure 2 - The Mars Rover Viewer Application Uses the Swing Application Framework Library.
 

In order to bundle all of the libraries into a single JAR file, you will need to edit the project's Ant file, which is called build.xml. Choose the Files tab in the Project window in the upper left, then expand the project tab if necessary to show the build.xml file. Double-click this file to edit it in the source-code editor pane and scroll down to the bottom. Now copy and paste the following text to the end, just before the final line, which is the </project> closing tag:

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

Note that you must change the following line in the Ant code above to match your particular project.

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

The property store.jar.name specifies the name of the JAR file that will be created in the store directory — change the value to whatever name you like. After you have changed this name, save the build.xml file. Figure 3 shows the additions to the build.xml file for the sample MarsRoverViewer project relative to the closing </project> tag.

Additions to the Project's build.xml File Relative to the Closing Tag
Figure 3 - Additions to the Project's build.xml File Relative to the Closing Tag.
 

From here, you can continue normal development, editing Java source files and compiling and running as necessary. Figure 4 shows the Mars Rover Viewer application fully built and running within the NetBeans IDE.

Running the Mars Rover Viewer Within the NetBeans IDE
Figure 4 - Running the Mars Rover Viewer Within the NetBeans IDE.
 

When you're ready to package all of the 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. Choose Run Target, scroll down to select Other Targets, then choose package-for-store, as shown in Figure 5, to execute the target. In this case, package-for-store is the name of the Ant target that you created previously in the project build.xml file.

Executing the package-for-store Ant Target From the Files Tab Inside the NetBeans IDE
Figure 5 - Executing the package-for-store Ant Target From the Files Tab Inside the NetBeans IDE.
 

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 6.

The Final JAR File, Ready to Upload to the Java Warehouse
Figure 6 - The Final JAR File, Ready to Upload to the Java Warehouse.
 

That's all there is to it — you can use this JAR file to upload to the Java Warehouse. Note that if you get an error stating that the dist/lib directory was not found, you should check that you have indeed included other libraries in your project, as this is the location where their JAR files will be stored.

If you'd like to verify that the JAR file contains the appropriate libraries, you can use one of three methods:

  • Use the command-line tool: jar tvf (filename).
  • Use a number of OS-based tools to inspect the JAR files (or ZIP files, if you change the extension).
  • Double-click on the JAR file to be sure that the Java runtime can execute it.

For More Information

 
 

Rate This Article

 

Discussion

We welcome your participation in our community. Please keep your comments civil and on point. You can optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.