Home : Course Map : Chapter 5 : Java :
Jar Files
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

A useful technique for organizing class files is to combine them into a file called a JAR or Java Archive.

JAR files are based on the ZIP archiving and compression tools that have been around for many years. JARs provide a number of advantages :

  • Compression helps for faster loading.
  • Loading a single large JAR file instead of several small class files reduces the unavoidable overhead that occurs for each network transfer.
  • Internally, the ZIP technique maintains the same directory structure as used for packages.

For the example discussed in the discussion of import, the applet TestABCApplet used classes in two packages. Here we use the jar tool, which works similarly as a ZIP tool, to create a JAR that holds the TestABCApplet class file and all of the package files that it uses. (See Tip below for info on using the jar tool.)

c:\...\myApps>jar -cvf TestABCApplet.jar TestABCApplet.class mypack
added manifest
adding: TestABCApplet.class(in = 1109) (out= 648)(deflated 41%)
adding: mypack/(in = 0) (out= 0)(stored 0%)
adding: mypack/extrapack/(in = 0) (out= 0)(stored 0%)
adding: mypack/extrapack/TestC.class(in = 250) (out= 203)(deflated 18%)
adding: mypack/extrapack/TestC.java(in = 129) (out= 93)(deflated 27%)
adding: mypack/TestA.class(in = 237) (out= 194)(deflated 18%)
adding: mypack/TestA.java(in = 111) (out= 81)(deflated 27%)
adding: mypack/TestB.class(in = 237) (out= 192)(deflated 18%)
adding: mypack/TestB.java(in = 110) (out= 79)(deflated 28%)

C:\...\myApps>  

Then we can use the following applet tag with the archive attribute to obtain the JAR. The JVM in the browser will look in the JAR file for TestABCApplet.class and the required package class files.

TestABCApplet.java
(Output goes to browser's Java console.)
  <applet code="TestABCApplet.class"
    archive="../../Code/Java/FileOrg/myApps/TestABCApplet.jar"
    width="150" height="64">
  </applet>

The jar tool provides various options (see below) including the ability to display the contents of a JAR file. Here we use the jar tool to display some of the files in the rt.jar, which holds the core language classes:

Note that the files are listed with their directory structure included. For example, the java/applet/* files correspond to the java.applet package.

Application in a JAR

You can also pack the classes for an application into a JAR and then run directly with it. For example, if you placed the class files for the application MyApp into MyApp.jar, then you would run the program with

  c:\> java -jar MyApp.jar

To indicate which of the class files holds the main() method, the JAR files needs to include a short Manifest file with the text line

  Main-Class: MyApp

If the manifest file name is Manifest, then to create the jar file use the -m option as in:

  c:\> jar -cvfm MyApp.jar Manifest MyApp.class

An optional approach to running from the jar, especially useful when you need to access classes in several jar files, is to use the the classpath option -cp as in:

  c:\> java -cp MyApp.jar MyApp

or in the case of several additional JAR files

  c:\> java -cp MyApp.jar;MyHelp.jar;MyUtilities.jar MyApp

More information about using JAR files can be found in the following resources listed below.

References & Web Resources

Tips: Java Tool Options

If you don't have a reference book handy, you can obtain a short list of options for a tool (these come comes with the development kit) by just typing the tool name without any arguments. Here we see the options available for the jar tool.

> jar

Usage: jar {ctxu}[vfm0M] [jar-file] [manifest-file] [-C dir] files ...

Options:

-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file

If any file is a directory then it is processed recursively. The manifest file name and the archive file name needs to be specified in the same order the 'm' and 'f' flags are specified.

Example 1: to archive two class files into an archive called classes.jar: jar cvf classes.jar Foo.class Bar.class

Example 2: use an existing manifest file 'mymanifest' and archive all the files in the foo/ directory into 'classes.jar': jar cvfm classes.jar mymanifest -C foo/ .

 

Last update: Mar.23.04

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.