Sie sind auf Seite 1von 7

Guide to Creating and Running a Jar File in Java | Baeldung

Start Here Courses  ▼ Guides  ▼ About  ▼

Guide to Creating and Running a Jar


File in Java
Last modified: November 13, 2019

by baeldung Java Maven

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.

2. Java Program Setup

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:

1 public static void main(String[] args) {


2     System.out.println("Hello Baeldung Reader!");
3 }

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.

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:

1 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:

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

3.3. Updating the Contents

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:

1 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:

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:

1 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:

1 jar cvfm JarExample.jar com/baeldung/jar/example_manifest.txt com/baeldung/jar/*.class


2 added manifest
3 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.

1 mvn package

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

4.2. Indicating the Main Class

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>

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.

1 <modelVersion>4.0.0</modelVersion>
2 <artifactId>spring-boot</artifactId>
3 <packaging>jar</packaging>
4 <name>spring-boot</name>

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

1 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:

1 <properties>
2     <start-class>com.baeldung.jar.JarExample</start-class>
3     <!-- Other properties -->
4 </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

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:

1 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:

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

Using path separators instead of package format also works:

1 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:

1 jar tf JarExample.jar
2 META-INF/
3 META-INF/MANIFEST.MF
4 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:

1 unzip -p JarExample.jar META-INF/MANIFEST.MF


2 Manifest-Version: 1.0
3 Created-By: 1.8.0_31 (Oracle Corporation)
4 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.

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.

I just announced the new Learn Spring course, focused on the


fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

 newest  oldest  most voted

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

Comments are closed on this article!

CATEGORIES SERIES ABOUT

SPRING JAVA “BACK TO BASICS” TUTORIAL ABOUT BAELDUNG


REST JACKSON JSON TUTORIAL THE COURSES
JAVA HTTPCLIENT 4 TUTORIAL CONSULTING WORK
SECURITY REST WITH SPRING TUTORIAL META BAELDUNG
PERSISTENCE SPRING PERSISTENCE TUTORIAL THE FULL ARCHIVE
JACKSON SECURITY WITH SPRING WRITE FOR BAELDUNG
HTTP CLIENT-SIDE EDITORS
KOTLIN OUR PARTNERS
ADVERTISE ON BAELDUNG

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]

Das könnte Ihnen auch gefallen