eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

eBook – Maven – NPI (cat=Maven)
announcement - icon

Get up to speed with the core of Maven quickly, and then go beyond the foundations into the more powerful functionality of the build tool, such as profiles, scopes, multi-module projects and quite a bit more:

>> Download the core Maven eBook

1. Overview

Usually, it’s convenient to bundle many Java class files into a single archive file.

In this tutorial, we’re going to cover the ins and outs of working with jar – or Java ARchive – files in Java. 

Specifically, we’ll take a simple application and explore different ways to package and run it as a jar. We’ll also answer some curiosities like how to easily read a jar’s manifest file along the way.

2. Java Program Setup

Before we can create a runnable jar file, our application needs to have a class with a main method. This class provides our entry point into the application:

public static void main(String[] args) {
    System.out.println("Hello Baeldung Reader!");
}

3. Jar Command

Now that we’re all set up let’s compile our code and create our jar file.

We can do this with javac from the command line:

javac com/baeldung/jar/*.java

The javac command creates JarExample.class in the com/baeldung/jar directory. We can now package that into a jar file.

3.1. Using the Defaults

To create the jar file, we are going to use the jar command.

To use the jar command to create a jar file, we need to use the c option to indicate that we’re creating a file and the f option to specify the file:

jar cf JarExample.jar com/baeldung/jar/*.class

3.2. Setting the Main Class

It’s helpful for the jar file manifest to include the main class.

The manifest is a special file in a jar located the META-INF directory and named MANIFEST.MFThe manifest file contains special meta information about files within the jar file.

Some examples of what we can use a manifest file for include setting the entry point, setting version information and configuring the classpath.

By using the e option, we can specify our entry point, and the jar command will add it to the generated manifest file.

Let’s run jar with an entry point specified:

jar cfe JarExample.jar com.baeldung.jar.JarExample com/baeldung/jar/*.class

3.3. Updating the Contents

Let’s say we’ve made a change to one of our classes and recompiled it. Now, we need to update our jar file.

Let’s use the jar command with the option to update its contents:

jar uf JarExample.jar com/baeldung/jar/JarExample.class

3.4. Setting a Manifest File

In some cases, we may need to have more control over what goes in our manifest file. The jar command provides functionality for providing our own manifest information.

Let’s add a partial manifest file named example_manifest.txt to our application to set our entry point:

Main-Class: com.baeldung.jar.JarExample

The manifest information we provide we’ll be added to what the jar command generates, so it’s the only line we need in the file.

It’s important that we end our manifest file with a newline. Without the newline, our manifest file will be silently ignored.

With that setup, let’s create our jar again using our manifest information and the option:

jar cfm JarExample.jar com/baeldung/jar/example_manifest.txt com/baeldung/jar/*.class

3.5. Verbose Output

If we want more information out of the jar command, we can simply add the v option for verbose.

Let’s run our jar command with the v option:

jar cvfm JarExample.jar com/baeldung/jar/example_manifest.txt com/baeldung/jar/*.class
added manifest
adding: com/baeldung/jar/JarExample.class(in = 453) (out= 312)(deflated 31%)

4. Using Maven

4.1. Default Configuration

We can also use Maven to create our jar. Since Maven favors convention over configuration, we can just run package to create our jar file.

mvn package

By default, our jar file will be added to the target folder in our project.

4.2. Indicating the Main Class

We can also configure Maven to specify the main class and create an executable jar file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin.version}</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.baeldung.jar.JarExample</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

5. Using Spring Boot

5.1. Using Maven and Defaults

If we’re using Spring Boot with Maven, we should first confirm that our packaging setting is set to jar rather than war in our pom.xml file.

<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot</artifactId>
<packaging>jar</packaging>
<name>spring-boot</name>

Once we know that’s configured, we can run the package goal:

mvn package

5.2. Setting the Entry Point

Setting our main class is where we find differences between creating a jar with a regular Java application and a fat jar for a Spring Boot application. In a Spring Boot application, the main class is actually org.springframework.boot.loader.JarLauncher.

Although our example isn’t a Spring Boot application, we could easily set it up to be a Spring Boot console application.

Our main class should be specified as the start class:

<properties>
    <start-class>com.baeldung.jar.JarExample</start-class>
    <!-- Other properties -->
</properties>

We can also use Gradle to create a Spring Boot fat jar.

6. Running the Jar

Now that we’ve got our jar file, we can run it. We run jar files using the java command.

6.1. Inferring the Main Class

Since we’ve gone ahead and made sure our main class is specified in the manifest, we can use the -jar option of the java command to run our application without specifying the main class:

java -jar JarExample.jar

6.2. Specifying the Main Class

We can also specify the main class when we’re running our application. We can use the -cp option to ensure that our jar file is in the classpath and then provide our main class in the package.className format:

java -cp JarExample.jar com.baeldung.jar.JarExample

Using path separators instead of package format also works:

java -cp JarExample.jar com/baeldung/jar/JarExample

6.3. Listing the Contents of a Jar

We can use the jar command to list the contents of our jar file:

jar tf JarExample.jar
META-INF/
META-INF/MANIFEST.MF
com/baeldung/jar/JarExample.class

6.4. Viewing the Manifest File

Since it can be important to know what’s in our MANIFEST.MF file, let’s look at a quick and easy way we can peek at the contents without leaving the command line.

Let’s use the unzip command with the -p option:

unzip -p JarExample.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_31 (Oracle Corporation)
Main-Class: com.baeldung.jar.JarExample

7. Conclusion

In this tutorial, we set up a simple Java application with a main class.

Then we looked at three ways of creating jar files: using the jar command, with Maven and with a Maven Spring Boot application.

After we created our jar files, we returned to the command line and ran them with an inferred and a specified main class.

We also learned how to display the contents of a file and how to display the contents of a single file within a jar.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)