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

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.

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.

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:

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.

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.

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

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.

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.