JAR archives

JAR archives are ZIP archives with a different extension. They contain a hierarchy of files and directories. In essence a JAR file can take the place of a directory containing its contents. This has many uses including the distribution of related classes as a single file.

The JDK includes a jar program modeled after the Unix tar program that will bundle up files and directories into a JAR archive. For example, suppose your homework directory contains the file Trivia.class.

Now suppose that Trivia is in the package edu.poly.utopia.eharold.games. Also suppose that the directory /home/users/eharold/homework/ contains (in various sub-directories) all the files and directories necessary to run the program edu.poly.utopia.eharold.games.Trivia. You can pack up everyhting in the edu package into a JAR archive like this:

$ cd /home/users/eharold/homework
$ jar cvf eharold.jar edu

The edu directory and all its contents are recursively stored in the archive named eharold.jar. The name of the archive is unimportant, only its contents.

This archive may now be added to the class path like this, or in whatever fashion you normally add directories to the class path on your platform of choice:

$ setenv CLASSPATH $CLASSPATH:eharold.jar

The main thing to note here is that the JAR file is now taking the place of an entire directory hierarchy.

In Java 1.2 you can also place it in the ext directory of your jre/lib directory.

Either way, all the classes in the archive will be added to the class path and will be accessible to your programs.

Previous | Next | Top | Cafe au Lait

Copyright 1999, 2002 Elliotte Rusty Harold
Last Modified October 3, 2002