LinuxCommandLibrary

jar

Package Java classes into JAR archives

TLDR

Recursively archive all files in the current directory into a .jar file

$ jar cf [file.jar] *
copy

Unzip .jar/.war file to the current directory
$ jar -xvf [file.jar]
copy

List a .jar/.war file content
$ jar tf [path/to/file.jar]
copy

List a .jar/.war file content with verbose output
$ jar tvf [path/to/file.jar]
copy

SYNOPSIS

jar { ctxui } [ options ] [ jar-file ] [ manifest-file ] [ entry-point ] [ -C dir ] files...

PARAMETERS

-c
    Create new archive

-t
    Display table of contents

-x
    Extract named (or all) files

-u
    Update existing archive

-i
    Generate index information

-f
    Specify archive file name

-m
    Include manifest information from file

-e
    Specify application entry point

-0
    Store only; no ZIP compression

-M
    Do not create manifest file

-v
    Verbose output

-C dir
    Change to directory before adding files

-JX
    Pass option X to underlying Java

--help
    Display help

--version
    Display version

DESCRIPTION

The jar command is Java's primary tool for creating, extracting, listing, and updating JAR (Java Archive) files. JAR files are ZIP-based archives specifically designed for Java applications and libraries, containing class files, resources, manifests, and metadata.

Common uses include packaging executable JARs for deployment, bundling dependencies, and creating modular libraries. Operations are specified via single-letter modes like c for create, x for extract, or t for listing contents. It supports manifest files for metadata such as main class entry points, permissions, and package sealing.

Key features: compression (ZIP-compatible), verbose output, directory traversal with -C, no-manifest mode, and index generation for multi-JAR setups. Unlike generic zip, jar handles Java-specific attributes like signatures and extensions. It's essential for Java developers building distributables, especially self-contained apps via -e for entry points.

Invocation requires JDK installation; it's not a native Linux binary but a shell script wrapper invoking Java.

CAVEATS

Requires JDK; JARs are ZIP-compatible but may need unzip for non-Java tools. Unsigned JARs pose security risks in applets. Large archives can hit memory limits.

COMMON EXAMPLE

jar cvfe app.jar MainClass -C src .
Creates executable JAR with verbose output.

MANIFEST FORMAT

Plain text file with headers like Main-Class: com.example.Main
Package sealing via Name/Sealed.

HISTORY

Introduced in JDK 1.0 (1996) as jar utility, inspired by zip. Evolved with Java versions: manifest support in 1.1, signed JARs in 1.2, slimmed index in 1.7. Maintained in OpenJDK; syntax stable since early releases.

SEE ALSO

java(1), javac(1), javadoc(1), zip(1), unzip(1)

Copied to clipboard