Java .jar files are archive files, like .zip files, that can contain a collection of Java artifacts including class files, source code, supporting documentation, etc. You can execute Java .jar files by adding a manifest to the .jar file that specifies the name of the main application class. This section provides step-by-step instructions that show you how to create and run an executable .jar file.
The following steps are required to create an executable .jar file:
¥Step 1: Create a Java program and save the resulting .class files in the proper package structure
¥Step 2: Create a manifest file specifying the fully-qualified name of the main application class
¥Step 3: Use the jar command to create the .jar file
Using the Java development environment of your choice, create a Java project as described in the previous sections of this chapter. Be sure to save the resulting .class files in the proper package directory structure. For this example, I will use SampleClass.class and ApplicationClass.class. Both of these files are saved in the following package directory structure:
com/pulpfreepress/jfa/chapter1
The only issue to note here is that if you are using Microsoft Windows the slashes would be replaced with backslashes like this:
com\pulpfreepress\jfa\chapter1
Using a text editor create a text file that contains a line specifying the fully-qualified name of the main application class. In this example, the fully-qualified name of ApplicationClass is this:
com.pulpfreepress.jfa.chapter1.ApplicationClass
Therefore, the entry in the manifest file would then look like this:
Main-class: com.pulpfreepress.jfa.chapter1.ApplicationClass
(IMPORTANT NOTE: Type a carriage return after this line. Failure to do so results in some Microsoft Windows systems reporting a “Failed to load Main-Class manifest attribute” error.)
Save the manifest file using any filename you choose. You do not have to provide a filename extension. For this example, I saved the manifest file with the name mainclass .
Now that you have created the manifest file, you are ready to use the jar command. The jar command takes the following format:
According to this jar command format, if you created the com package structure in your working directory, and you created the manifest file named mainclass , and you wanted to name the jar file MyApp.jar , the actual jar command would look like this:
jar cmf mainclass MyApp.jar com
This would use the mainclass manifest file to create the MyApp.jar file using the class files located in the com.pulpfreepress.jfa.chapter1 package.
To execute the .jar file, use the java command with the -jar switch as is shown here:
java -jar MyApp.jar
Figure 2-38 shows the MyApp.jar file being executed with the java command and the -jar option.
Figure 2-38: Executing the MyApp.jar File Using the java Command
The Java jar command lets you create executable .jar files. To create an executable .jar file you need to create a manifest file that provides the fully-qualified name of the main application class. Be sure to add an extra line to the manifest so that it work in the Microsoft Windows environment. To execute the .jar file use the java command with the -jar option.