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:
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:
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:
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:
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:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
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:
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:
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.
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.MF. The 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 u 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 m 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.
