Java JAR Files: Creating, Running and Configuring with the Manifest
Package Java applications and libraries as JAR files: the jar tool and its options, the META-INF/MANIFEST.MF manifest, executable JARs with Main-Class and java -jar, multi-release JARs, modern --long options, JDK_JAVA_OPTIONS, fat JARs and jpackage installers.
kvi5hnuCode Sprout team member12 min readOn this page
When you ship an application, you want to give users one file, not a directory tree full of class files. JAR (Java ARchive) files were designed for this. A JAR can hold class files plus any other files the program needs, such as images, sounds, configuration and translations. It's compressed with the familiar ZIP format.
Libraries come as JARs too. When you add a dependency like Jackson or JUnit to a project, your build tool downloads JAR files and puts them on the class path (previous article).

Creating JAR Files with the jar Tool
The jar tool lives in the JDK's bin directory, next to javac. The classic command to create a JAR is
jar cvf jarFileName file1 file2 ...
for example
jar cvf CalculatorClasses.jar *.class icon.png
The general form is jar options file1 file2 .... The single-letter options are modeled on the Unix tar command, so they're written without a dash:
| Option | Meaning |
|---|---|
c | Create a new archive and add files. Directories are processed recursively. |
C | Temporarily change directory before adding the following files: jar cvf app.jar -C classes . adds everything under classes/ without the classes/ prefix. |
e | Set the entry point (main class) in the manifest. |
f | The next argument is the JAR file name. Without it, jar writes to standard output (or reads from standard input). |
i | Create an index file (historical; ignored by modern JDKs). |
m | Add entries from a manifest file. |
M | Don't create a manifest. |
t | Display the table of contents. |
u | Update an existing JAR. |
v | Verbose output. |
x | Extract files (all files, or the ones you name). |
0 | Store without ZIP compression. |
A complete round trip, from a project whose sources are in src/:
javac -d classes src/com/example/app/*.java # compile into classes/
jar cvf app.jar -C classes . # package everything under classes/
jar tf app.jar # list the contents
META-INF/
META-INF/MANIFEST.MF
com/
com/example/
com/example/app/
com/example/app/Main.class
com/example/app/Util.class
Note that jar added a META-INF/MANIFEST.MF file automatically. Extract an archive with jar xf app.jar. Since a JAR is a ZIP file, unzip -l app.jar works too.
The Manifest
Besides classes and resources, every JAR contains a manifest describing special features of the archive. It's called MANIFEST.MF and lives in the META-INF directory. The minimal legal manifest is a single line:
Manifest-Version: 1.0
The jar tool also writes Created-By (the JDK that built it). Manifest entries are Name: value lines grouped into sections:
- The first section is the main section, which applies to the whole JAR.
- Later sections describe named entries (individual files or packages). Each starts with a
Nameline. Sections are separated by blank lines.
Manifest-Version: 1.0
Main-Class: com.example.app.Main
(lines describing this archive)
Name: com/example/app/Main.class
(lines describing this file)
Name: com/example/app/
(lines describing this package)
To add your own lines, put them in a text file and pass it with the m option:
jar cfm MyArchive.jar manifest.mf -C classes .
To add entries to the manifest of an existing JAR, use u and m:
printf "Implementation-Version: 1.2.0\n" > add.mf
jar ufm app.jar add.mf
Useful main-section attributes:
| Attribute | Purpose |
|---|---|
Main-Class | The class whose main method java -jar runs |
Class-Path | Space-separated relative URLs of other JARs this JAR needs, e.g. lib/json.jar lib/log.jar |
Implementation-Title, Implementation-Version, Implementation-Vendor | Version info, readable at runtime with Package.getImplementationVersion() |
Multi-Release | true for a multi-release JAR (below) |
Add-Opens, Add-Exports | Grant deep-reflection access to JDK internals for an executable JAR |
Enable-Native-Access | ALL-UNNAMED lets code in an executable JAR call native code via the FFM API without warnings (Java 22+) |
Automatic-Module-Name | A stable module name for a non-modular library JAR |
The full format is documented in the JAR File Specification in the JDK documentation.
Executable JAR Files
To make a JAR runnable, tell the launcher which class contains main. Either use the e option:
jar cvfe MyProgram.jar com.example.app.Main -C classes .
or put a Main-Class line into your manifest file:
Main-Class: com.example.app.Main
Write the class name, without a .class extension. Either way, users start the program with
java -jar MyProgram.jar
Running com.example.app.Main on Java 25
base version of Util
With -jar, the JAR is the class path: -cp options and the CLASSPATH variable are ignored. Dependencies must come from the manifest's Class-Path attribute or be packed into the JAR. If you want to pick the main class yourself, use the normal launcher instead: java -cp MyProgram.jar com.example.app.Main.
Double-clicking and native launchers
Depending on the operating system, users may be able to launch an executable JAR by double-clicking it. On Windows, Java installers can associate .jar files with javaw -jar (javaw is like java but doesn't open a console window). On macOS, a JAR can be opened with the installed Java runtime.
But a JAR doesn't feel like a native application, and it requires a Java runtime on the user's machine. Better options today:
jpackage(in the JDK since Java 16) builds native installers and app images:.msi/.exeon Windows,.dmg/.pkgon macOS,.deb/.rpmon Linux. It bundles a trimmed Java runtime, so users don't need to install Java.jlinkcreates a custom, minimal runtime image containing only the modules your app needs.- Third-party wrappers such as Launch4j turn a JAR into a Windows
.exethat finds or bundles a JVM. - GraalVM Native Image compiles an application ahead of time into a standalone native executable.
jpackage --name MyApp --input target --main-jar myapp.jar --main-class com.example.app.Main
Fat JARs
Server applications and command-line tools often ship as a single "fat" (uber) JAR that contains the application and all its dependencies, so java -jar app.jar just works. Build plugins create them: the Maven Shade or Assembly plugin, the Gradle Shadow plugin, and Spring Boot's plugin (which nests dependency JARs and uses its own launcher). Remember that merging JARs can create conflicts (duplicate classes or META-INF/services files) that these plugins have to resolve.
Multi-Release JAR Files
Since Java 9, a library sometimes needs different code for different Java versions. Strong encapsulation made some internal APIs inaccessible, while newer releases offer better public replacements (for example, VarHandle instead of sun.misc.Unsafe). Multi-release JARs handle this: one JAR, with version-specific class files.
For backward compatibility, the version-specific classes go in META-INF/versions/N, and the manifest contains Multi-Release: true:
com/example/app/Main.class
com/example/app/Util.class
META-INF/
├─ MANIFEST.MF (contains Multi-Release: true)
└─ versions/
└─ 21/
└─ com/example/app/Util.class

A Java 8 runtime knows nothing about META-INF/versions and loads the base classes. Java 9 and later check the versions directories: on Java 21 or later, the JVM uses versions/21/.../Util.class and falls back to the base version for classes without an override.
Build it by compiling each variant with --release into its own directory, then combining them:
javac -d classes8 --release 8 src/com/example/app/*.java
javac -d classes21 --release 21 -cp classes8 src21/com/example/app/Util.java
jar --create --file mr.jar --main-class com.example.app.Main \
-C classes8 . --release 21 -C classes21 .
$ java -jar mr.jar # on Java 25
Running com.example.app.Main on Java 25
Java 21+ version of Util
To add versioned classes to an existing JAR: jar uf MyProgram.jar --release 21 -C classes21 ..
Notes:
javac --release Ncompiles against the exact API of release N, so you can't accidentally use newer methods. It's better than-source/-target. The-doption creates the output directory if needed. JDK 21 to 25 compilers support--release 8and later. Release 8 is deprecated (you get an "obsolete" warning) and will eventually be dropped.- The public API must be the same for all versions. Multi-release JARs exist only so that one version of a library works on multiple JDK releases. If you add features or change the API, publish a new version of the JAR instead.
- Some tools look at the base version by default.
javap -cp mr.jar com.example.app.Utilshows the base class; usejavap --multi-release 21 -cp mr.jar com.example.app.Utilto see the Java 21 variant.
A Note on Command-Line Options
JDK tools traditionally used single dashes with multi-letter names (java -jar, javac -Xlint:unchecked -classpath ...). The exception was jar, with its tar-style options and no dash (jar cvf).
Since Java 9, JEP 293 has been moving the JDK toward the GNU style common on Linux: double dashes for long names, and single-letter shortcuts for common ones, like ls --human-readable / ls -h:
| Classic | Modern long form |
|---|---|
java -version | java --version |
java -classpath ... / -cp | java --class-path ... |
jar cvf app.jar ... | jar --create --verbose --file app.jar ... |
jar tf app.jar | jar --list --file app.jar |
jar xf app.jar | jar --extract --file app.jar |
jar cvfe app.jar Main ... | jar --create --file app.jar --main-class Main ... |
| (none) | jar --describe-module --file app.jar, jar --validate --file app.jar |
The module path option is --module-path, with the shortcut -p (see the modules part).
Arguments of -- options can be separated by a space or =:
java --class-path /home/dev/build/classes ...
java --class-path=/home/dev/build/classes ...
The = form helps when options pass through another tool that can't handle spaces.
Common questions
Why does java -jar app.jar say "no main manifest attribute"?
The manifest has no Main-Class entry. Rebuild the JAR with jar --create --file app.jar --main-class com.example.Main -C classes ., or configure your build tool to set the main class. Alternatively, run java -cp app.jar com.example.Main without -jar.
Can I put dependency JARs inside my JAR?
The standard class loader can't load classes from JARs nested inside a JAR. Either list the dependencies in the manifest's Class-Path (relative to the JAR's location), build a fat JAR that unpacks their classes, or use a framework launcher such as Spring Boot's, which knows how to read nested JARs.
Is jar still needed when I use Maven or Gradle?
Not directly. The build tools create JARs for you. Knowing the jar tool still helps when inspecting archives (jar tf), debugging manifests, or packaging something by hand in a script.
What does --release 8 warn about on a modern JDK?
JDK 25 still accepts --release 8 but prints "source value 8 is obsolete and will be removed in a future release". Libraries that need to support Java 8 should plan for a time when the newest JDKs can't compile for it. Many popular libraries now require Java 11 or 17 as a minimum.
Should I use jpackage or a fat JAR?
For desktop apps that end users install, use jpackage, which creates a native installer with a bundled runtime. For servers and containers, a fat JAR or a layered container image with a JDK base image is typical. For CLI tools that need fast start-up, consider a GraalVM native image or the JDK's AOT cache.
Summary
- A JAR is a ZIP file with class files, resources and a manifest. Create it with
jar cvf app.jar -C classes ., list it withjar tf, and extract it withjar xf. - The manifest
META-INF/MANIFEST.MFholdsName: valueattributes in sections. Add your own withjar cfm(create) orjar ufm(update), and end the last line with a newline. - An executable JAR has a
Main-Classattribute (jar cvfeor--main-class) and runs withjava -jar app.jar, which ignores-cp. - Use
jpackagefor native installers and fat-JAR plugins for single-file deployments. - Multi-release JARs (
Multi-Release: true,META-INF/versions/N) let one library version run on several JDK releases with the same API. Compile each variant with--release N. - Modern JDK tools accept GNU-style
--longoptions (--class-path,--create,--file).JDK_JAVA_OPTIONSpasses extra options tojava.
Practice Questions
Your compiled classes are in out/. Write the commands to create app.jar whose main class is com.shop.Main, and run it.
jar cvfe app.jar com.shop.Main -C out . (or jar --create --file app.jar --main-class com.shop.Main -C out .), then java -jar app.jar.
java -jar app.jar prints "no main manifest attribute". Name two possible causes.
(1) The JAR was built without a Main-Class (no e option, no manifest line). (2) The manifest file's Main-Class line lacks a terminating newline, so it was ignored. A misspelled attribute name has the same effect.
You run java -cp lib/json.jar -jar app.jar and get NoClassDefFoundError for a JSON class. Why?
With -jar, the class path comes only from the JAR and its manifest; -cp is ignored. Add Class-Path: lib/json.jar to the manifest, build a fat JAR, or run with java -cp app.jar:lib/json.jar com.shop.Main.
Why must all versions in a multi-release JAR have the same public API?
Clients compile against one JAR and must work on any JDK. If the API differed between versions, code compiled on one JDK could fail with missing methods on another. Multi-release JARs exist only to swap implementations for different JDKs. New features belong in a new library version.
What's the advantage of javac --release 17 over javac -source 17 -target 17 on a JDK 25 compiler?
--release 17 also compiles against the Java 17 API, so using a method added in Java 21 is a compile error. With -source/-target, the code compiles against the JDK 25 library and may fail at runtime on Java 17 with NoSuchMethodError.
Previous: Java Package Access and the Class Path
Related reading
- Java Package Access and the Class Path
- Java Packages: Package Names, Imports and Static Imports
- Modular JARs and Reflective Access in Java: opens and open modules
- Java Module requires and exports: Dependencies and Exported Packages
- Your First Java Program Explained: Classes, main, Statements and Comments

More in Core Java I
Java volatile, Atomics, Deadlocks and ThreadLocal: Safe Sharing Between Threads
Share data between Java threads without bugs: memory visibility and volatile, final fields, AtomicInteger and compareAndSet, LongAdder, lazy holder initialization, safe publication, lock-ordering deadlocks, ThreadLocal, InheritableThreadLocal and ScopedValue.
Java Condition Objects and the synchronized Keyword: await, signalAll, wait and notify
Make threads wait for a condition safely with Condition.await and signalAll, then learn Java's built-in alternative: synchronized methods and blocks, intrinsic locks, wait and notifyAll, choosing lock objects, client-side locking, and the monitor concept.
Java Race Conditions and ReentrantLock: Synchronizing Shared Data
Why unsynchronized threads corrupt shared data, shown step by step with a lost-update race, and how to fix it with ReentrantLock: the lock/try/finally idiom, critical sections, reentrancy and hold counts, tryLock, fair locks, and the mistakes that cause bugs.
Java invokeAll, invokeAny and Fork-Join: Running Groups of Tasks
Coordinate many concurrent tasks in Java: invokeAll to wait for every result, invokeAny to take the first success, timeouts, ExecutorCompletionService for results in completion order, structured concurrency, and the fork-join framework with RecursiveTask, RecursiveAction and work stealing.
Discussion
Sign in to ask a question or share what you learned.