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 {c|u|x|t|i|d} [options] [jar-file] [manifest-file] [entry-point] [-C dir] files...

PARAMETERS

-c
    Creates a new JAR file.

-u
    Updates an existing JAR file by adding new files or replacing existing ones.

-x
    Extracts files from a JAR file. If no files are specified, all files are extracted.

-t
    Lists the table of contents for a JAR file without extracting its contents.

-i
    Generates an index file for a JAR archive (useful for faster class loading in certain scenarios).

-d
    Calculates and displays the difference between a JAR file and a directory. (Java 11+)

-f jar-file
    Specifies the name of the JAR file to be created or manipulated. Required for most operations.

-v
    Generates verbose output to standard output or standard error while processing.

-m manifest-file
    Includes manifest information from the specified pre-existing manifest file.

-0
    Stores the files in the JAR file without using ZIP compression (zero compression).

-C dir
    Changes the directory to the specified dir and includes subsequent files from there. Useful for archiving specific directory structures.

-e main-class
    Specifies the application entry point for a standalone executable JAR file. This sets the 'Main-Class' attribute in the manifest.

-M
    Do not create a manifest file for the JAR archive.

-J option
    Passes option directly to the Java runtime environment (JVM) that jar is running on.

--module-version version
    When creating a modular JAR, specifies the module's version. (Java 9+)

--release version
    When creating a multi-release JAR, specifies the target release for subsequent files. (Java 9+)

DESCRIPTION

The jar command is a utility included with the Java Development Kit (JDK) that allows users to create, view, update, and extract files from Java Archive (JAR) files.

A JAR file is a package file format typically used to aggregate many Java class files, associated metadata, and resources (text, images, etc.) into a single file for distribution. It is built on the ubiquitous ZIP file format and can optionally include a manifest file that contains metadata about the archive's contents, such as the application's main class for executable JARs, version information, and more.

JAR files are crucial for Java application deployment, acting as a convenient way to bundle libraries, entire applications, or modules. They streamline distribution, allow for digital signing for security, and facilitate efficient loading of application components.

CAVEATS

The jar command is primarily designed for Java-related packaging and does not support all features of the underlying ZIP format, such as arbitrary file comments or encryption. While it can create executable JARs, they require a Java Runtime Environment (JRE) to be installed on the user's system to run. Large archives can still take significant time to create or extract, and managing manifest files can sometimes be complex for advanced configurations.

HISTORY

The jar utility has been a fundamental part of the Java Development Kit (JDK) since its early versions (JDK 1.1). It was introduced to standardize the packaging and distribution of Java applications, making it easier for developers to deploy their code. Over the years, its core functionality has remained consistent, but it has evolved to support new features of the Java platform, such as modular JARs and multi-release JARs with the advent of Java 9, and the ability to differentiate between JARs and directories (Java 11+).

SEE ALSO

java(1), javac(1), zip(1), unzip(1), jarsigner(1), jmod(1)

Copied to clipboard