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 [options] [destination] [source files]

PARAMETERS

c
    Create a new archive.

t
    List the table of contents for an archive.

x
    Extract files from an archive.

u
    Update an existing archive.

f jar-file
    Specify the archive file name. If omitted when creating an archive, jar output will be written to standard output. If omitted when extracting, jar input will be read from standard input.

v
    Generate verbose output on standard error.

0
    Store only; use no ZIP compression.

M
    Do not create a manifest file for the archive.

m manifest-file
    Include manifest information from manifest-file.

i index-file
    Generate index information for the specified jar files.

C directory
    Change to the specified directory and include the following file.

--release version
    Creates a multi-release JAR file.

DESCRIPTION

The jar command is a utility provided with the Java Development Kit (JDK) for creating and managing Java Archive (JAR) files. JAR files are used to bundle multiple Java class files, resources, and metadata into a single archive file for distribution and deployment.

The command allows you to create JAR files, list their contents, extract files from them, and update existing JAR files. It also provides options for specifying a manifest file, which contains metadata about the archive, such as the entry point for an executable JAR file.

The jar command is essential for building and deploying Java applications and libraries. It simplifies the distribution and deployment process by packaging all necessary files into a single archive, making it easier to manage dependencies and deploy applications to different environments.

CAVEATS

The jar command relies on the JDK being installed and properly configured in your system's PATH. Incorrect use of options can lead to corrupted or unusable JAR files.

MANIFEST FILES

Manifest files (typically named META-INF/MANIFEST.MF within the JAR) are crucial for specifying metadata about the JAR file. They can define the main class to execute, classpath dependencies, and other attributes that influence the JAR's behavior at runtime.

EXECUTABLE JARS

A JAR file can be made executable by specifying the Main-Class attribute in its manifest file. This allows the JAR to be run directly from the command line using java -jar myapp.jar. The jar command can be used to create such executable JARs.

MULTI-RELEASE JARS

Multi-release JARs allow for including different versions of class files for different Java runtime environments. This is useful for supporting older and newer Java versions with a single JAR file. The --release option facilitates creating these JARs.

HISTORY

The jar command has been a core utility in the Java Development Kit (JDK) since its early versions. Its purpose has always been to bundle Java class files and resources for easier distribution and deployment. Over time, it has been extended with additional options to support features like manifest files and multi-release JARs.
The command has seen continuous usage since the beginning of Java development.

SEE ALSO

javac(1)

Copied to clipboard