LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

jar

Java Archive tool

TLDR

Create JAR file
$ jar cf [output.jar] [files...]
copy
Create with manifest
$ jar cfm [output.jar] [MANIFEST.MF] [files...]
copy
Extract JAR file
$ jar xf [archive.jar]
copy
List contents
$ jar tf [archive.jar]
copy
Create executable JAR
$ jar cfe [output.jar] [MainClass] [files...]
copy
Update JAR file
$ jar uf [archive.jar] [newfiles...]
copy
Extract specific file
$ jar xf [archive.jar] [path/to/file]
copy
List contents verbosely
$ jar tvf [archive.jar]
copy
Create JAR from directory using -C to change base dir
$ jar cf [output.jar] -C [build/classes] .
copy

SYNOPSIS

jar [options] [manifest] destination input-files

DESCRIPTION

jar is the Java Archive tool. It packages Java class files, resources, and metadata into a single JAR file for distribution and deployment.JAR files use ZIP format with a manifest (META-INF/MANIFEST.MF) containing metadata. Executable JARs specify a main class in the manifest.

PARAMETERS

c

Create new archive.
x
Extract archive.
t
List table of contents.
u
Update existing archive.
f file
Specify archive filename.
m manifest
Include manifest file.
e class
Set entry point (main class).
v
Verbose output.
0
Store only (no compression).
M
Do not create a manifest file.
i
Generate index information for specified JAR files.
C dir
Change to directory before including the following files.
--release VERSION
(Java 9+) Place following files in a versioned directory for multi-release JARs.

MANIFEST EXAMPLE

$ Manifest-Version: 1.0
Main-Class: com.example.Main
Class-Path: lib/dependency.jar
copy

CAVEATS

Order of flags matters when using legacy (non-dashed) syntax. The manifest file must end with a newline. Paths are relative to the current directory unless -C is used. In Java 9+ the `--module-version` and `--release` flags add module/multi-release support.

HISTORY

The jar tool has been part of the JDK since Java 1.1 (1997). It's essential for Java deployment, evolving to support modules in Java 9+ and multi-release JARs.

SEE ALSO

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

Copied to clipboard
Kai