Working with JAR

Java 8 min min read Updated: Mar 30, 2026 Beginner
Working with JAR
Beginner Topic 5 of 25

Working with JAR

In Java, as projects grow, developers often need a clean and convenient way to package compiled classes, resources, and configuration files into a single distributable unit. That is where a JAR file becomes important.

JAR stands for Java Archive. It is a file format used to bundle multiple Java class files, metadata, images, property files, and other resources into one compressed file. JAR files make it easier to distribute, reuse, deploy, and run Java applications.

Key Concept: A JAR file is a packaged archive that combines Java class files and related resources into a single file for easy distribution, reuse, and execution.

What is a JAR File?

A JAR file is similar to a ZIP file, but it is specifically designed for Java applications and libraries. It usually has the extension:

text .jar

A JAR file may contain:

  • compiled Java class files
  • resource files such as images and properties files
  • metadata like manifest file
  • package structure of the application

Instead of distributing many separate files and folders, developers can distribute one JAR file.

Why JAR Files are Used

JAR files are used widely in Java development for the following reasons:

  • Easy distribution: many files can be packaged into one
  • Reusability: libraries can be shared as JARs
  • Portability: applications can be moved easily between systems
  • Compression: files are stored in compressed form
  • Execution: executable JARs can run directly

Real-World Use of JAR Files

In real Java projects, JAR files are commonly used in these situations:

  • packaging a Java application for deployment
  • sharing utility libraries with other developers
  • including third-party dependencies such as database drivers
  • distributing desktop Java applications
  • running Spring Boot and other Java-based applications

For example:

  • mysql-connector.jar → database connectivity library
  • log4j.jar → logging library
  • app.jar → packaged Java application

Basic Structure of a JAR File

A JAR file is not just a random collection of files. It usually follows a structured format.

Typical contents of a JAR file:

text myapp.jar ├── META-INF/ │ └── MANIFEST.MF ├── com/ │ └── example/ │ └── Main.class ├── config.properties └── logo.png

Here:

  • META-INF contains metadata
  • MANIFEST.MF stores manifest information
  • class files remain in package structure
  • other resources can also be added

What is a Manifest File?

Every JAR file can contain a special file called:

text META-INF/MANIFEST.MF

This file stores metadata about the JAR. It may contain:

  • version details
  • author information
  • classpath details
  • main class name for executable JAR

Example manifest:

text Manifest-Version: 1.0 Main-Class: com.example.Main

If the Main-Class attribute is present correctly, the JAR can be run directly.

Types of JAR Files

JAR files are commonly of two major types:

  • Library JAR
  • Executable JAR

1. Library JAR

A library JAR contains reusable classes and methods that can be used by another Java program. It is not directly run by itself in most cases.

Example:

text math-utils.jar

This JAR may contain utility methods for mathematical calculations.

2. Executable JAR

An executable JAR contains the compiled application code along with a manifest that defines the main class. It can be run directly using the Java command.

Example:

bash java -jar myapp.jar

Creating a Simple Java Program for JAR Packaging

Let us first create a simple Java class.

java package com.example; public class Main { public static void main(String[] args) { System.out.println("Hello from JAR file"); } }

Save this file as:

text com/example/Main.java

Compiling the Java File

Compile the source file using:

bash javac com/example/Main.java

After compilation, the directory contains:

text com/example/Main.class

Creating a JAR File

Java provides the jar command for creating JAR files.

Basic Syntax

bash jar cf myapp.jar com

Here:

  • jar → command-line tool
  • c → create a new JAR file
  • f → specify the JAR file name
  • myapp.jar → output JAR file
  • com → folder to include

This creates:

text myapp.jar

Viewing the Contents of a JAR File

To see what is inside a JAR file, use:

bash jar tf myapp.jar

Output may look like:

text META-INF/ META-INF/MANIFEST.MF com/ com/example/ com/example/Main.class

Extracting a JAR File

To extract the contents of a JAR file, use:

bash jar xf myapp.jar

This extracts all files into the current directory.

Creating an Executable JAR File

A normal JAR file only stores files. To make it executable, you must define the main class in a manifest file.

Step 1: Create Manifest File

Create a file named manifest.txt with the following content:

text Main-Class: com.example.Main

Make sure there is a new line at the end of the manifest file, otherwise the JAR command may not read it properly.

Step 2: Create Executable JAR

bash jar cfm myapp.jar manifest.txt com

Here:

  • c → create
  • f → file name
  • m → include custom manifest file

Step 3: Run Executable JAR

bash java -jar myapp.jar

Output

text Hello from JAR file

Understanding Common JAR Command Options

The jar command uses flags. Some important options are:

Option Meaning
c Create a new JAR
f Specify file name
t List contents of JAR
x Extract files from JAR
v Verbose output
m Include manifest file

Verbose JAR Creation Example

To create a JAR and display the files being packaged:

bash jar cvf myapp.jar com

The v option gives detailed output.

Using JAR as a Library

JAR files are frequently used as libraries in other Java programs. For example, let us create a utility class and package it into a JAR.

java package com.utils; public class MathUtil { public static int add(int a, int b) { return a + b; } }

Compile it:

bash javac com/utils/MathUtil.java

Create JAR:

bash jar cf mathutil.jar com

Now another Java program can use this JAR.

Using the Library JAR in Another Program

java import com.utils.MathUtil; public class Demo { public static void main(String[] args) { int result = MathUtil.add(10, 20); System.out.println("Sum: " + result); } }

Compile with JAR in Classpath

bash javac -cp mathutil.jar Demo.java

Run with JAR in Classpath

bash java -cp .;mathutil.jar Demo

On Linux or macOS, classpath separator is : instead of ;.

bash java -cp .:mathutil.jar Demo

Classpath and JAR Files

When Java needs classes from external JAR files, the JAR must be included in the classpath.

Classpath tells the JVM where to look for class files.

Example:

bash javac -cp library.jar MyProgram.java java -cp .;library.jar MyProgram

If the JAR is not included in the classpath, Java will show class not found errors.

Running JAR from IDEs

Modern IDEs like NetBeans and Eclipse can export a project as a JAR without manually typing commands.

In NetBeans

  • build the project
  • generated JAR appears in the dist folder

In Eclipse

  • use Export option
  • choose Runnable JAR File or JAR File export

Although IDEs simplify the process, understanding manual JAR commands is important for interviews and backend deployment work.

Fat JAR and Thin JAR

In modern Java applications, especially in frameworks like Spring Boot, two related terms are often used:

  • Thin JAR: contains only the application classes
  • Fat JAR or Uber JAR: contains the application plus all dependencies

A fat JAR is easier to deploy because everything is bundled together.

Common Errors While Working with JAR

1. No Main Manifest Attribute

If you try to run:

bash java -jar myapp.jar

and receive an error like:

text no main manifest attribute, in myapp.jar

it means the manifest file does not define the main class correctly.

2. Class Not Found Exception

This happens when the required class or dependency JAR is missing from the classpath.

3. Wrong Package Structure

If the package structure inside the JAR does not match the Java package declaration, the program may fail.

4. Incorrect Manifest Formatting

Manifest file lines must be correctly formatted. Missing newline at the end may cause problems.

5. Platform-Specific Classpath Confusion

Windows uses ; while Linux/macOS uses : as classpath separator.

Best Practices While Working with JAR

  • use meaningful JAR names such as student-app.jar
  • keep package structure clean and consistent
  • verify manifest file carefully for executable JARs
  • test JAR outside the IDE before deployment
  • understand classpath when using external libraries
  • use build tools like Maven or Gradle for complex JAR packaging

JAR vs ZIP

Feature JAR ZIP
Purpose Java application/library packaging General file compression
Manifest support Yes No special Java manifest
Executable with JVM Yes, if configured No
Java package structure Preserved Generic

Interview-Oriented Points

  • JAR stands for Java Archive
  • It is used to bundle class files and resources into one file
  • JAR files may be library JARs or executable JARs
  • Manifest file stores metadata such as main class
  • java -jar is used to run executable JARs
  • Classpath is required when external JAR dependencies are used
  • A fat JAR contains both application code and dependencies

Conclusion

JAR files are one of the most important parts of Java application packaging and distribution. They allow developers to combine classes, resources, and metadata into a single file that can be reused or executed easily.

Whether you are building a small utility library or a full Java application, understanding how to create, inspect, execute, and use JAR files is a core Java skill. It is especially valuable in real-world development, deployment, and interviews.

Quick Summary: A JAR file packages Java classes and resources into a single archive. It is used for library reuse, application distribution, and executable Java deployments.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators