Java - JAR

Updated: 2019-01-04

Basic Operations

Options:

  • c: create;

  • f: file specified in the command line, instead of stdin/stdout;

  • t: view the table of the contents;

  • x: extract;

  • u: update;

  • e: specify the entrypoint

  • v: verbose output;

  • 0(zero): without compressing;

  • M: without generating default Menifest file;

  • m: include an existing Menifest file;

  • -C: change current directory;

Note: (from oracle.com)

  • The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8.
  • 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.
  • When you create a JAR file, the time of creation is stored in the JAR file. Therefore, even if the contents of the JAR file do not change, when you create a JAR file multiple times, the resulting files are not exactly identical. You should be aware of this when you are using JAR files in a build environment. It is recommended that you use versioning information in the manifest file, rather than creation time, to control versions of a JAR file

Create

Create a jar from input-files:

$ jar cf foo.jar <input-files>

Create a jar and add existing manifest to default:

$ jar cmf <existing-manifest> foo.jar <input-files>

Change to bar subdirectory, and all files in that directory:

$ jar cf foo.jar -C bar .

View Contents

View the table of the contents

$ jar tf foo.jar

More information

$ jar tvf foo.jar

Extract

$ jar xf foo.jar
$ jar xf foo.jar <archived-files>

Update

Update files in jar

$ jar uf foo.jar <input-files>

Update files in images folder(change to images before update)::

$ jar uf foo.jar -C images <input-files>

Run

$ java -jar foo.jar

Manifest

There should be only one manifest file, and the path/name is fixed:

META-INF/MANIFEST.MF

Content format: header: value pairs.

Manifest-Version

The manifest version is set in default file when generated

Manifest-Version: 1.0

Main-Class(Entry Point)

Set the entry point for the jar, specify a class with public static void main(String[] args)

Main-Class: <classname>

Save it in Manifest.txt and run:

$ jar cfm foo.jar Manifest.txt <input-files>

Alternatively, use e to specify the entrypoint during creation::

$ jar cfe foo.jar <classname> <input-files>

Class-Path

Class-Path: foo.jar bar.jar baz.jar

Version Info

The versioning headers should appear directly beneath the Name header for the package:

Name: java/util/
Specification-Title: Java Utility Classes
Specification-Version: 1.2
Specification-Vendor: Example Tech, Inc.
Implementation-Title: java.util
Implementation-Version: build57
Implementation-Vendor: Example Tech, Inc.

Note that the package name must end with a "/".

Seal

Definition: all classes defined in that package must be archived in the same JAR file. A sealed JAR specifies that all packages defined by that JAR are sealed unless overridden on a per-package basis.

Sealed: true