Course contentsShow
← Java
Lesson 685 of 2,87016. Packages, Imports, and JAR FilesFree lesson

Creating a JAR File with the jar Command

Using the jar command-line tool to create JAR files, specifying options like -c (create), -f (file), -v (verbose), and -m (manifest).

Creating a JAR File with the jar Command

What you'll learn: How to use the jar command-line tool to bundle your compiled Java classes into a single archive file.

What Is the jar Command?

The jar command is a utility that comes with the JDK, allowing you to create, update, extract, and inspect JAR files from the command line. Think of it as a specialized tool for packaging Java applications—like a factory machine that takes your individual class files and wraps them into a neat, portable container.

Basic Syntax

The general syntax follows this pattern:

jar [options] [jar-file] [input-files]

Essential Options

-c (create)

Creates a new JAR file. This is your starting point for packaging.

-f (file)

Specifies the name of the JAR file to create. Without this, output goes to standard out (usually not what you want).

-v (verbose)

Prints detailed information about what the jar command is doing—helpful for confirming which files are being added.

-m (manifest)

Includes a custom manifest file with metadata about the JAR (like which class contains the main method).

Creating Your First JAR

To create a JAR file named myapp.jar from compiled classes in the current directory:

jar -cf myapp.jar *.class

For verbose output showing each file added:

jar -cvf myapp.jar *.class

To include classes from a package directory structure:

jar -cvf myapp.jar com/mycompany/myapp/*.class

Key Takeaway: The jar command with options -c (create), -f (filename), -v (verbose), and -m (manifest) lets you bundle compiled Java classes into distributable JAR files directly from the command line.