[Texas PLT logo]

COMP 202: Principles of Object-Oriented Programming II

  JAR Files  

JAR (Java ARchive) files are special files that contain Java applications.  They generally contain either compiled Java code or Java source code; when properly designed, they also allow Java applications to be easily distributed and executed.

Despite the fancy name (and its resemblance to the tar extension used by the Unix tape archive command), JAR files are actually zip files in disguise.  Zip is a popular file compression format developed by PKWare.  This means you can also create and modify JAR files with your favorite zip compression tool. JAR files are intended to be created with the jar utility from the JDK, however.

More detailed information on JAR files is available on the Sun web site: http://java.sun.com/docs/books/tutorial/jar/

Extracting JAR files

Since JAR files are just zip files, you should be able to use your favorite zip compression tool (the command unzip works under Unix) to decompress them.  You can also use the jar command with the following syntax (the 'x' means to extract and the 'f' means to use the given JAR file):

jar xf jarfile.jar

Creating source code JAR files

To create a simple, non-executable JAR file use the following command:

jar cfv jarfile.jar list of files to-put-in-jar-file

The 'c' means create a JAR file, the 'f' means to save the JAR file to a file rather than printing it to stdout, and the 'v' means to be more verbose, particularly if an error occurs.  The following is a more concrete example.

jar cfv listSource.jar AList.java EmptyList.java NEList.java

The above command will create a jar file called listSource.jar, compress AList.java, EmptyList.java, NEList.java, and add the compressed filed to listSource.jar.

Passing filenames on the Unix command line and JAR creation examples

When creating JAR files, the last arguments on the command line are the files to include in the JAR file.  Unix is very powerful and allows many tricks in passing the filenames.  One way to give it filenames is to simply give it wildcards like this:

jar -cfv hw02.jar *.java docs/*.html docs/*.png

That command will create a simple source code JAR file named hw02.jar containing all *.java files in the current directory and all *.png and *.html files in the docs/ subdirectory.  This assumes that you told StructureBuilder to generate documentation in the docs/ subdirectory, of course.

Simply putting a '.' (a period) in place of the file listing tells jar to include the current directory and all subdirectories:

jar cfv hw02.jar .

Creating executable JAR files

Creating an executable JAR file that allows the application to be run with the java -jar command is slightly more difficult.

First, make a file called "manifest" with the following contents (on a PC, it may be easier to make a file called "manifest.txt"):

Main-Class: classDirectory/ClassName

Be sure that there is is a newline at the end of the above statement! That is, be sure that there is one more line after the above line in the manifest file. Failure to do this will cause the manifest to not work properly.

This identifies the main class of your Java application - in other words, the one with the main() method. The "classDirectory/" part is optional and is only required if the main class is in a subdirectory.  The "ClassName" part must not include the .class extension.

Next, run the jar command with the following syntax:

jar cfvm filename.jar manifest files-to-put-in-jar-file

or if your manifest is called manifest.txt:

jar cfvm filename.jar manifest.txt files-to-put-in-jar-file

 

The 'm' means to use the manifest file stored in the file with the name of the parameter after the filename.jar file parameter (in this case, "manifest").  The files that you put in should be the compiled .class files.

All the compiled .class files must be included for this to work properly. The filenames should be separated by spaces. The wildcard "*" can be used specify a set of files, e.g. "myPackage/*.class".

This is the type of JAR file you are most likely to create when sharing your Java application with friends. The resulting JAR file can be run with the java -jar command listed below.

Using JAR files

In order to run a compiled Java application packaged in a JAR file, simply use the following syntax:

java -jar jarfile.jar

If you have JDK properly set up on a Windows machine, you should be able to run the JAR files by simply double-clicking on them.

 

  JAR Files  

URL: http://www.clear.rice.edu/comp202/08-fall/info/jar.shtml
Copyright © 2008-2010 Mathias Ricken and Stephen Wong