Jar

Alberto Ferrari
Ingegneria dell'Informazione, UniPR

Oracle documentation

Packaging Programs in JAR Files

  • The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive file.
  • Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.
  • JAR file benefits:
    • Security: You can digitally sign the contents of a JAR file
    • Compression
    • Packaging for extensions: The extensions framework provides a means by which you can add functionality to the Java core platform
  • JAR files are packaged with the ZIP file format (lossless data compression)

Creating a JAR File

jar cf jar-file input-file(s)
  • Options and arguments:
    • c indicates that you want to create a JAR file
    • f indicates that you want the output to go to a file rather than to stdout
    • jar-file name of resulting JAR file. By convention .jar
    • input-file(s) is a space-separated list of files that you want to include in your JAR file (can contain the wildcard * symbol).
    • If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.
  • This generate a compressed JAR and a default manifest file

Options

  • m Used to include manifest information from an existing manifest file
jar cmf existing-manifest jar-file input-file(s)

Run JAR Files as Applications

java -jar jar-file
  • -jar flag tells the launcher that the application is packaged in the JAR file format.
  • 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
  • classname, is the name of the class that is the application's entry point.

Manifest Files

  • The manifest is a special file that can contain information about the files packaged in a JAR file
  • When you create a JAR file, 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
  • The default manifest file simply contains the following:
Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
  • Manifest's entries take the form of "header: value" pairs.

Manifest addictions

  • Use the m command-line option to add custom information to the manifest during creation of a JAR file.
  • Typically, modifying the default manifest involves adding special-purpose headers to the manifest that allow the JAR file to perform a particular desired function.
  • You must first prepare a text file containing the information you wish to add to the manifest.
  • Warning: The text file from which you are creating the manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
jar cfm jar-file manifest-addition input-file(s)

Setting Application's Entry Point

Main-Class: classname
  • classname is the name of the class that is your application's entry point
    • class having a method with signature public static void main(String[] args)

Example

  • We first create a text file named Manifest.txt with the following contents:
Main-Class: MyPackage.MyClass
  • Warning: The text file must end with a new line or carriage return.
  • We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class

Example (1)

  • Classes
public class A {
    public static void main(String[] args) {
        System.out.println("Class A: main");
        new B();
    }
}

public class B {
    public B() {
        System.out.println("new B object");
    }
}

Example(2)

  • Manifest.txt
Main-Class: A
javac A.java
jar cfm A.jar Manifest.txt A.class B.class
java -jar A.jar

Setting an Entry Point with the JAR Tool

  • 'e' flag (for 'entrypoint') creates or overrides the manifest's Main-Class attribute
  • Use it to specify the application entry point without editing or creating the manifest file.
jar cfe app.jar MyApp MyApp.class
  • If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter
  • For example, if Main.class is in a package called foo the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class

Adding Classes to the JAR File's Classpath

  • You may need to reference classes in other JAR files from within a JAR file.
  • You specify classes to include in the Class-Path header in manifest file
Class-Path: jar1-name jar2-name directory-name/jar3-name

Alberto Ferrari
Ingegneria dell'Informazione, UniPR
www.ce.unipr.it/~aferrari/