Guide To Creating and Running A Jar File in Java Baeldung
Guide To Creating and Running A Jar File in Java Baeldung
I just announced the new Learn Spring course, focused on the fundamentals of
Spring 5 and Spring Boot 2:
>> CHECK OUT THE COURSE
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.
We use cookies to improve your experience with the site. To find out more, you can read the full Privacy and Cookie Policy Ok
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.
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
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:
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:
1 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.
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:
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:
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
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:
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:
1 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:
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:
4. Using Maven
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.
1 mvn package
By default, our jar file will be added to the target folder in our project.
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
We can also configure Maven to specify the main class and create an executable jar file.
1 <plugin>
2 <groupId>org.apache.maven.plugins</groupId>
3 <artifactId>maven-jar-plugin</artifactId>
4 <version>${maven-jar-plugin.version}</version>
5 <configuration>
6 <archive>
7 <manifest>
8 <mainClass>com.baeldung.jar.JarExample</mainClass>
9 </manifest>
10 </archive>
11 </configuration>
12 </plugin>
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.
1 <modelVersion>4.0.0</modelVersion>
2 <artifactId>spring-boot</artifactId>
3 <packaging>jar</packaging>
4 <name>spring-boot</name>
1 mvn package
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:
1 <properties>
2 <start-class>com.baeldung.jar.JarExample</start-class>
3 <!-- Other properties -->
4 </properties>
Now that we've got our jar file, we can run it. We run jar files using the java command.
Since we've gone ahead and made sure our main class is specified in the manifest, we can use the -jar option
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
of the java command to run our application without 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:
We can use the jar command to list the contents of our jar file:
1 jar tf JarExample.jar
2 META-INF/
3 META-INF/MANIFEST.MF
4 com/baeldung/jar/JarExample.class
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:
7. Conclusion
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
Both the plain Java example and the Spring Boot example are available over on GitHub.
Ron Wheeler
Have you done a tutorial on how arguments can be specified and used when running a jar file?
Guest https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html is pretty short and misses a lot.
https://www.javatpoint.com/command-line-argument is the top return on a Google search but is really not complete.
https://www.leepoint.net/JavaBasics/methods/methods-15-arguments.html not much better JNDI and properties files can
be used. – How to set values at run time – How to access from Java How does Spring differ from a pure Java app? I just
had to try to figure the simple cases out for a JavaFX utility that I wrote where I wanted to be able to implement… Read
more »
0 10 months ago
Loredana Crusoveanu
Hi Ron,
Guest
That’s an interesting topic – we’ll add it to the content calendar.
In the meantime, we do have a topic on command line arguments with Spring Boot:
https://www.baeldung.com/spring-boot-command-line-arguments
Cheers.
0 8 months ago
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]
Guide to Creating and Running a Jar File in Java | Baeldung
TERMS OF SERVICE
PRIVACY POLICY
COMPANY INFO
CONTACT
https://www.baeldung.com/java-create-jar[03/01/2020 11:09:50]