JAR Files Objectives: 1. Archiving and Packaging Java Code jar 3. Example Applet

Download Report

Transcript JAR Files Objectives: 1. Archiving and Packaging Java Code jar 3. Example Applet

JAR Files
Objectives: 1. Archiving and Packaging Java Code
2. The jar Program
3. Example Applet
JAR Files
 JAR file archives provide packaging for
applications, applets, & JavaBeans
 Multiple classes can be packaged in JAR files
 JAR Files specified within HTML
 Manifest file
 JAR files use ZIP compression format
JAR Files:
 provide a delivery mechanism for JavaBeans,
packaging all the necessary resources together in a
single file. Multiple Beans may be contained in a
single JAR file, and the file may also contain an
optional manifest which lists the contents.
 help transport Beans over the Internet in easy-tomanage packages, and they are also used to archive
applets for more efficient downloading performance.
The jar Utility
 Creates JAR files
 Adds to existing archives
 Extracts items in an archive
 Lists archive contents
 Syntax:
jar {ctux} [vfm0M] [jar-file] [manifest-file] files
jar Command
jar {ctux} [vfm0M] [jar-file] [manifest-file] files
 jar-file specifies the name of the archive file. No
required naming conventions exist for Java archive
files, although you will most likely see the file
extension .jar associated with them.
 The manifest-file argument is a file containing a list of
the jar file contents. A manifest file is required when
using the m command line option.
More...
jar Command
jar {ctux} [vfm0M] [jar-file] [manifest-file] files
 files to be archived are listed at the end of the command and are
separated by whitespace.
 Wildcard characters are accepted
 directory names are followed recursively, i.e., all files in
subdirectories will also be archived.
 File names are relative to the directory from which you run the jar
command
 special –C option introduced in Java 1.2 to change the relative
directory.
 forward or backward slashes are accepted, making the jar
command operating-system independent.
jar command line options
Only one of the following
c: Create a new archive file
t: List the table of contents for the archive file
u: Update the contents of the archive or manifest
x: Extract named files (or all) from the archive
Other options can be combined or omitted
v: Verbose output
f: Specify [jar-file]
m: Include information from [manifest-file]
0: Do not use compression
M: Do not create a manifest file for the archive entries
Examples of jar command
 Create an archive called Simulator.jar that contained
the single class Machine.class
 jar cf Simulator.jar Machine.class
 List the table of contents of Simulator.jar using
verbose output
 jar tvf Simulator.jar
 Extract all contents of Simulator.jar
 jar xvf Simulator.jar
JAR Files & HTML
Improve Applet download performance
 compression of JAR files allows efficient downloading of applets over the
Web.
 entire archive may be downloaded in a single HTTP transaction.
 avoids reconnect each time a specific .class file or resource is needed.
ARCHIVE Tag
<APPLET ARCHIVE=“Simulator.jar” CODE=Stock.class WIDTH=350
HEIGHT=250>
</APPLET>
 filename is relative to the location of the HTML page.
 still need a CODE tag to indicate which class within the archive starts
the applet.
The Manifest
The manifest file exists to provide information about the
contents of its associated jar file.
 Automatically created
 if not specified otherwise, named manifest.mf
 placed in the archive under a directory called /META-INF
 M option in jar command suppresses creation.
 Lists the entries in the archive
 First entry identifies manifest version Example:
Manifest-Version: 1.0
Created-By: 1.5.0 (Sun Microsystems Inc.)
 applies to the entire JAR file
Manifest Contents
 Subsequent sections must begin with
 Name: <entry>
 describe individual elements JavaBeans, files, packages, and URLs.
 Each section of the manifest file refers to an entry in the archive and is
separated by a blank line.
 Attribute-value pairs of the format attribute: value are used in each
section to give information about that particular entry.
Name: Calculator.class
lines describing this file
Name: edu/weber/mypkg
lines describing this package
Manifest Contents
 Attribute/Value pairs
 Note Java-Bean attribute in example:
Name: StockBroker/Simulator/Stock.class
Digest-Algorithms: MD5 SHA
MD5-Digest: wuXrtYN-56Nn4rtDFewq=-tr
SHA-Digest: fg67tKdkfR65-SRkdrslegvY-tyK
Java-Bean: True
Custom Manifest File Indicating Beans
Manifest-Version: 1.0
Name: StockBroker/Simulator/Stock.class
Java-Bean: True
Name: StockBroker/Simulator/Helper.class
Java-Bean: False
Name: StockBroker/Simulator/Price.class
Java-Bean: True
Creating the Archive File
 create an archive file with a customized manifest file by using both the m
and f options and giving the required arguments as shown in the slide.
The generated manifest.mf file in the archive should have the information
given in Simulator.mf (which you write).
>jar cfm Simulator.jar Simulator.mf
StockBroker/Simulator/Stock.class
StockBroker/Simulator/Helper.class
StockBroker/Simulator/Price.class
 if the jar options contain both the letters f and m, the JAR and manifest
files listed in the command line should be in the same order. For example,
if f comes before m, the JAR filename should come before the manifest
filename.
Examples
See Composites using a jar in the
Mastermind Directory
See Animation Demo using a jar at:
http://acad.kutztown.edu/~spiegel/cis421/Animation/AppletJAR/ball.htm
 inner classes specified in a manifest