Java Top

Finally announcing my next course. The intro price of the upcoming “Learn Spring” course will permanently increase by $50 at end-of-day today:

>> HAVE A LOOK

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.

Both the plain Java example and the Spring Boot example are available over on GitHub.

Java bottom

Finally announcing my next course. The intro price of the upcoming “Learn Spring” course will permanently increase by $50 at end-of-day today:

>> HAVE A LOOK

Leave a Reply

avatar
  Subscribe  
Notify of