Jar Files

General Information

Introduction and Purpose

Jar File in the Java Glossary by Roedy Green

The Java Archive (JAR) file format bundles multiple files into a single archive file. The file format is based on ZIP and the ZLIB compression format, so the Java jar tool can be used as a (command line) replacement for pkzip that works on Unix platforms. Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications -- not just class files.


Jar files can be elements on a class path:

java -cp dir/dir1:lib1.jar:dir/dir2:lib2.jar:. Application

Using the Jar Tool

Creating a JAR file jar cf jar-file input-file(s)
Viewing the contents of a JAR file jar tf jar-file
Extracting the contents of a JAR file jar xf jar-file
Extracting specific files from a JAR file jar xf jar-file archived-file(s)
Merging manifest informationjar cmf manifest-addition jar-file input-file(s)
Updating existing jarjar uf jar-file input-file(s)
Creating a JAR file (Java 1.6) jar cef entry jar-file input-file(s)
To run an application packaged as a JAR file
(version 1.2 -- requires 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>

Manifest entries:

mainMain-Class: Main
Main-Class: pack.ClassName
class pathClass-Path: http://www.univ.edu/file.jar
Class-Path: file:/directory/subdirectory/
Class-Path: file:/directory/subdirectory/file.jar
Class-Path: file1.jar file2.jar

Executing Jar Files

In version 1.2 (not earlier) of the JDK software, you can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The -jar flag tells the interpreter that the application is packaged in the JAR file format.

Before this command will work, however, the runtime environment needs to know which class within the JAR file is the application's entry point.

To indicate which class is the application's entry point, you must add a Main-Class header to the JAR file's manifest. The header takes the form:

Main-Class: classname
The header's value, classname, is the name of the class that's the application's entry point.
javac Main.java
echo "Main-Class: Main" >! mc.mf   # create a manifest file entry
jar -cvmf mc.mf main.jar Main.class
added manifest
adding: Main.class(in = 414) (out= 284)(deflated 31%)
rm mc.mf             # file no longer needed
java -jar main.jar   # jar file now executes without mentioning main

Applet in jar file:

<applet code=TicTacToe.class 
        width=120 height=120>


The syntax of a JAR URL is:
for example:

Accessing resources

final java.net.URL url = X.class.getResource ("/dir/image.png");
final ImageIcon icon = new ImageIcon (url);

Sealed Packages

In version 1.2 of the JavaTM platform, packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.

A package can be sealed by adding the Sealed header beneath the header naming the package that's to be sealed.:

Name: myCompany/myPackage/
Sealed: true

The default manifest created by the Jar tool does not contain any Sealed headers, of course, because packages are not sealed by default. To seal a package, you therefore have to add the Sealed header yourself. To insert the Sealed header in a JAR file's manifest, you first need to write a text file containing the appropriate headers. The file you write doesn't have to be a complete manifest file; it can contain just enough information for the Jar tool to know where and what information to merge into the default manifest.

Package Versioning

Version Specification

java.lang.Package (Java API documentation) (local copy)

class Main {
    public static void main(String[] args) {
        Package[] pkgs = Package.getPackages();
        for (int i=0; i<pkgs.length; i++) {
            System.out.println("  Specification Title:     " + pkgs[i].getSpecificationTitle());
            System.out.println("  Specification Version:   " + pkgs[i].getSpecificationVersion());
            System.out.println("  Specification Vendor:    " + pkgs[i].getSpecificationVendor());
            System.out.println("  Implementation Title:    " + pkgs[i].getImplementationTitle());
            System.out.println("  Implementation Version:  " + pkgs[i].getImplementationVersion());
            System.out.println("  Implementation Vendor:   " + pkgs[i].getImplementationVendor());
            if (pkgs[i].isSealed()) {
                System.out.println("  Sealed: true");

Java Extensions

Extension Mechanism from the Java Tutorial.

Reading and Writing Jar Files in Java

Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Tue Feb 20 14:37:17 EST 2007