Using and Developing jar Files

A jar file contains a collection of Java byte codes and possibly other resources, such as images. jar files provide a convenient way to distribute libraries of program code and standalone Java applications. Because they are compressed, jar files can also significantly reduce the file size of program code.

To run an application, you open a command prompt window and run the java command with the byte code file that contains the program's main method. Alternatively, if a program's byte codes are bundled in an executable jar file, you can launch the program just by double-clicking on the jar file's icon or running the command java –jar <jarfilename>.

To create an executable jar file, you must perform the following steps:

1.     Open a text editor and create a text file named manifest.tmp. This file should contain a line of code that specifies the name of the class that contains the main method of your Java application. For example, if your main class is named MyApplication, the line of code in the file should be

Main-Class: MyApplication

Save the file in the directory that contains your byte code files.

2.     At the command prompt run the jar command with the tags cfm, the name of the jar file, name of the manifest file, and the names of the byte code files as command line arguments. For example, the following command will generate an executable jar file named MyApplicayion.jar from all of the byte code files in the current directory, using the manifest file manifest.tmp:

jar cfm MyApplication.jar manifest.tmp *.class

3.     Test the resulting jar file by launching it.

A program library usually consists of one for more Java packages. You can use a library by placing the directories that contain the packages in your current working directory. However, this necessitates moving these directories every time you change to another working directory. A far better way to use (and distribute) a program library is to bundle its packages in a jar file. The jar file can then be placed in Java's system directory, where it is accessible from any working directory or from most commercial and free IDEs.

To create a jar file for a program library, you must perform the following steps:

1.     Place the package directory for the library in your current working directory. If there is more than one package in the library, they should already be organized in a hierarchy of subdirectories or as several directories at the same level. Note that the directories should contain just byte code files (files with a .class extension).

2.     At the command prompt run the jar command with the tag cvf, the name of the jar file, and the top-level package directory name. For example, if the package name is TurtleGraphics, you would run the command line

jar cvf TurtleGraphics.jar TurtleGraphics

 

3.     To test the resulting jar file, copy it to the appropriate places in JavaÕs system directory. On a PC running Windows XP, these two directories are

c:\Program Files\Java\jdk1.5.0\jre\lib\ext

c:\Program Files\Java\ jre1.5.0\lib\ext

On a Macintosh, this directory is Macintosh Hard Drive/Library/Java/Extensions. Compile and run a Java program that uses the library, after making sure that the library does not exist in the programÕs current working directory.