3.1 INTRODUCTION
JAR (Java Archive) is a platform-independent file format that aggregates many files into one. The JAR tool combines multiple files into a single JAR file. JAR is a general-purpose archiving and compression tool, based on ZIP and ZLIB compression format. However, JAR was designed mainly to facilitate the packaging of Java applets or applications into a single archive. A JAR file allows a programmer to efficiently deploy a set of classes and their associated resources. Multiple Java applets and their requisite components (.class files, images and sounds) can be bundled in a JAR file and subsequently downloaded to a browser in a single HTTP transaction, greatly improving the download speed. The JAR format also supports compression, which reduces the file size, further improving the download time.
The most common uses of JAR files are lossless data compression, archiving, decompression and archive unpacking. The general syntax of creating a JAR file is:
jar options files
The table given below lists common JAR files operations:
Operation | Command |
To create a JAR file | jar cf jar-file input-file(s) |
To view the contents of a JAR file | jar tf jar-file |
To extract the contents of a JAR file | jar xf jar-file |
To extract specifies files from a JAR file | jar xf jar-file archived file(s) |
To run an application packaged as a JAR file (requires the Main-class manifest header) | java - jar app.jar |
To invoke an applet packaged as a JAR file | <applet code = AppletClassName.class archive = “JarFileName.jar” width = width height = height> </applet> |
The next table lists various JAR command options:
The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)
The c
option indicates that programmer wants to create a JAR file. The f
option indicates that the programmer wants the output to go to a file rather than to stdout. The name that can be assigned to the resulting JAR file is jar-file
. Any file name can be used for JAR file. By convention, JAR filenames are given a .jar extension, though this is not required. The input-file(s) argument is a space separated list of one or more files that needs to be included in a JAR file. The input-file(s) argument can contain the wildcard *
symbol. If none of the “input-files” are directories, the contents of those directories are added to the JAR archive recursively.
This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.
A3.2 THE MANIFEST FILE
The manifest is a special file that can contain information about the files packaged in a JAR file. It is because of manifest file, JAR files support a wide range of functionality, including electronic signing, version control and package scaling.
When a JAR file is created, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF.
When a JAR file is created, the default manifest file simply contains the following:
Manifest-Version: 1.0 Created-By: 1.5.0 (Sun Microsystems Inc.)
These lines show that a manifest's entries take the form of “header: value” pairs. The name of a header is separated from its value by a colon. The default manifest confirms to version 1.0 of the manifest specification. The manifest file is used in Java beans for storing information that which files in JAR file are Java beans.
A3.3 EXAMPLES OF JAR COMMAND OPTION
1. jar cf abc.jar *.class *.gif
The command creates a JAR file named abc.jar that contains all of the .class and .gif files in the current directory.
2. jar cfm abc.jar man.mf *.class *.gif
The command creates an abc.jar using manifest file man.mf.
3. jar tf abc.jar
The command lists the contents of jar file abc.jar.
4. jar xf abc.jar
The command extracts the contents of jar file abc.jar and places in the current directory.
5. jar
—uf abc.jar file1.class
The command adds file1.class to the contents of abc.jar file, thus updating the JAR file.
6. jar
—uf abc.jar —C Xdir *
The command adds all files below directory Xdir to JAR file abc.jar.
7. jar uf abc.jar
—C classes demo.class
The command adds demo.class file from classes directory to abc.jar file.
8. jar uf abc.jar
—C classes . —C bin xyz.class
The command adds all files in classes directory as well as file xyz.class from bin directory to the JAR file abc.jar. If classes file contains two files demo1 and demo2, the following command jar tf foo.jar will give the following output:
META-INF/META-INF/MANIFEST.MF bar1 bar2 xyz.class
9. jar cvf bundle.jar *
The command adds all files in the current directory to the JAR file bundle. jar.
The v
option displays the name of the file as it is added to the bundle. jar
10. jar cvf bundle.jar audio classes images
The command adds all files in the directories audio, classes and images to the file bundle. jar file.
11. To run a Java application class file that is stored within a JAR file the – jar option can be used. For that a JAR file is to be created that contains application class file. This creates a text file named Manifest.txt with the following contents:
Main-class: demo
The text file must end with a new line or carriage return and there must a space after colon. A JAR file named MyJar.jar is then created by entering the following command:
jar cfm MyJar.jar Manifest.text demo.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Created -By: 1.5.0 (Sun Microsystems Inc.) Main-Class: demo
When the JAR file is run with the following command, the main
method of demo
executes:
java –jar MyJar.jar
3.145.173.199