Summary


Creating And Running Executable Jar Files

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.

Steps To Create 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

Step 1: Create Your Java Program

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 image from book SampleClass.class and image from book 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

Step 2: Create Manifest File

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 .

Step 3: Use The jar Command

Now that you have created the manifest file, you are ready to use the jar command. The jar command takes the following format:

image from book

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 image from book 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 image from book MyApp.jar file using the class files located in the com.pulpfreepress.jfa.chapter1 package.

Executing The jar File

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 image from book MyApp.jar file being executed with the java command and the -jar option.

image from book
Figure 2-38: Executing the MyApp.jar File Using the java Command

Quick Review

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.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net