jar - JAR File Tool Command and Options

This section describes the 'jar' command to manage JAR files. 'jar' command syntax and options are provided.

What Is "jar"? "jar" is a command line tool for managing JAR files.

"jar" has been included in JDK installation since JDK 1.0. And it is represented by the %java_home%\bin\jar.exe program file.

The "jar" tool has some interesting features:

You can run "jar" by typing in "jar" at the command prompt, if you have %java_home%\bin directory included in "path" the environment variable. If you want to know how to use the "jar" command, you can run the "jar --help" command:

C:\herong>jar --help

Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
jar creates an archive for classes and resources, and can manipulate or
restore individual classes or resources from an archive.

Examples:
# Create an archive called classes.jar with two class files:
jar --create --file classes.jar Foo.class Bar.class

# Create an archive using an existing manifest, with all the files in foo/:
jar --create --file classes.jar --manifest mymanifest -C foo/ .

# Create a modular jar archive, where the module descriptor is located in
# classes/module-info.class:
jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0
   -C foo/ classes resources

# Update an existing non-modular jar to a modular jar:
jar --update --file foo.jar --main-class com.foo.Main --module-version 1.0
   -C foo/ module-info.class
    
# Create a multi-release jar, placing some files in the META-INF/versions/9 
# directory:
jar --create --file mr.jar -C foo classes --release 9 -C foo9 classes

To shorten or simplify the jar command, you can specify arguments in 
a separate text file and pass it to the jar command with the at sign (@) 
as a prefix.

Examples:
# Read additional options and list of class files from the file classes.list
jar --create --file my.jar @classes.list


Main operation mode:

  -c, --create          Create the archive
  -i, --generate-index=FILE  
     Generate index information for the specified jar archives
  -t, --list            List the table of contents for the archive
  -u, --update          Update an existing jar archive
  -x, --extract         Extract named (or all) files from the archive
  -d, --describe-module      
     Print the module descriptor, or automatic module name

Operation modifiers valid in any mode:

  -C DIR                Change to the specified directory and include 
     the following file
  -f, --file=FILE       The archive file name. When omitted, either stdin 
     or stdout is used based on the operation
  --release VERSION     Places all following files in a versioned 
     directory of the jar (i.e. META-INF/versions/VERSION/)
  -v, --verbose         Generate verbose output on standard output

Operation modifiers valid only in create and update mode:

  -e, --main-class=CLASSNAME The application entry point for stand-alone
     applications bundled into a modular, or executable, jar archive
  -m, --manifest=FILE        
     Include the manifest information from the given manifest file
  -M, --no-manifest     Do not create a manifest file for the entries
  --module-version=VERSION    
     The module version, when creating a modular jar, or updating 
     a non-modular jar
  --hash-modules=PATTERN 
     Compute and record the hashes of modules matched by the given pattern 
     and that depend upon directly or indirectly on a modular jar being
     created or a non-modular jar being updated
  -p, --module-path          
     Location of module dependence for generating the hash

Operation modifiers valid only in create, update, and generate-index mode:

  -0, --no-compress     Store only; use no ZIP compression

Other options:

  -h, --help[:compat]   Give this, or optionally the compatibility, help
      --help-extra      Give help on extra options
      --version         Print program version

An archive is a modular jar if a module descriptor, 'module-info.class', 
is located in the root of the given directories, or the root of the jar 
archive itself. The following operations are only valid when creating a 
modular jar, or updating an existing non-modular jar: '--module-version', 
'--hash-modules', and '--module-path'. 

Mandatory or optional arguments to long options are also mandatory or 
optional for any corresponding short options. 

For more information, see "jar" reference page at https://docs.oracle.com/javase/10/tools/jar.htm.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

jar - The JAR File Tool

 JAR - Java Archive File Format

jar - JAR File Tool Command and Options

 "jar --create" - Creating New JAR File

 "jar --list" - Listing Files in JAR File

 "jar --extract" - Extracting Files from JAR File

 Managing JAR Files with WinZIP

 META-INF/MANIFEST.MF - JAR Manifest File

 Adding META-INF/MANIFEST.MF to JAR Files

 "jar -C" - Changing Input Directory

 Using JAR Files in Java Class Paths

 "jar --update" - Updating Class Files in JAR

 "jar --main-class" - Making JAR File Executable

 Creating Module JAR File

 "jar --module-version" - Updating Module Version in JAR

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB