How to Create an Executable JAR with Maven?

How to Create an Executable JAR with Maven?

To create an executable Maven JAR with dependencies, you can use the maven-assembly-plugin or spring-boot-maven-plugin. Edit the pom.xml file, then run the Maven clean package, and your jar will be created.

Table of Contents:

What is a JAR File in Java?

A JAR file (Java ARchive) is a file that is used to package multiple files into one single file. It is used to combine Java class files, images, sounds, and other resources. JAR files make it easier to share and distribute Java applications. 

Creating a runnable Maven JAR with dependencies can be done easily. You can use plugins like Assembly, Shade, or Spring Boot Maven Plugin, depending on your project type. For Windows, the JAR can be converted into an .exe using Launch4J. This process is often referred to as Java JAR to Windows EXE conversion.

Now, we will see the detailed steps of how to create a runnable JAR using Maven or an exe with different methods.

Create Executable JAR with Maven Assembly Plugin

The following steps should be followed to create an executable JAR with Maven using IntelliJ.

Step 1: Create or Open a Maven Project in IntelliJ

  • 1. Open IntelliJ IDEA
  • 2. Create New Project           
Creating a JAR Using the Maven Assembly Plugin-1

Now you will be able to see the pom.xml file and a src/main/java structure.

Step 2: Write Your Main Class

Create a simple Java class under src/main/java/com/example

package org.example;
public class Main {
   public static void main(String[] args) {
       System.out.println("Hello from Intellipaat ");
   }
}

Step 3: Add the Maven Assembly Plugin to pom.xml

Edit your pom.xml and add the following.

   <build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>

<!-- Maven Assembly Plugin to create a fat JAR -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.example.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Step 4: Build the Project in IntelliJ

Open the Maven window on the right sidebar, which will appear like an “M” tab. Expand it like project > Lifecycle > and then double-click on the package

Creating a JAR Using the Maven Assembly Plugin-2

Step 5: Find the Output JAR

Go to the target folder inside your project, where you will see a JAR.

Creating a JAR Using the Maven Assembly Plugin-3

Step 6: Run the JAR

Open a terminal and run:

java -jar target/Intellipaat-1.0-SNAPSHOT-jar-with-dependencies.jar

Output:

Creating a JAR Using the Maven Assembly Plugin-4
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

Create Executable JAR Using Maven Shade Plugin

The following steps should be followed to create executable JAR with Maven using IntelliJ.

Step 1: Create or Open a Maven Project in IntelliJ

1. Open IntelliJ IDEA

2. Create New Project         

Creating a JAR Using the Maven Shade Plugin-1

Now you will be able to see the pom.xml file and a src/main/java structure.

Step 2: Write Your Main Class

Create a simple Java class under src/main/java/com/example/Main.java

public class Main {
   public static void main(String[] args) {
       System.out.println("Hello from Intellipaat ");
   }
}

Step 3: Add the Maven Shade Plugin to pom.xml

Edit your pom.xml and add the following inside the <build><plugins> section:

<build>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Step 4: Build the Project in IntelliJ

Open the Maven window on the right sidebar, which will appear like an “M” tab. Expand it like project > Lifecycle > and then double-click on the package.

Creating a JAR Using the Maven Shade Plugin-2

Step 5: Find the Output JAR

Go to the target folder inside your project, where you will see a JAR like Intellipaat-1.0-SNAPSHOT-shaded.jar

Creating a JAR Using the Maven Shade Plugin-3

Step 6: Run the JAR

Open a terminal and run:

java -jar .Intellipaat-1.0-SNAPSHOT-shaded.jar

Output:

Creating a JAR Using the Maven Shade Plugin-4

Create Executable JAR Using the Spring Boot Maven Plugin

The following steps should be followed to create a Maven executable JAR file using IntelliJ.

Step 1: Create or Open a Maven Project in IntelliJ

  • 1. Open IntelliJ IDEA
  • 2. Create New Spring-Boot Project            
Creating a JAR Using the Spring Boot Maven Plugin-1

Now you will be able to see the pom.xml file and a src/main/java/com.example.demo structure.

Note: Spring Boot dependencies are added automatically when you create a new Spring project.

Step 2: Write Your Main Class

Create a simple Java class under src/main/java/com/example/demo.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
       System.out.println("Hello from INTELLIPAAT");
   }
}

Step 3: Build the Project in IntelliJ

Open the Maven window on the right sidebar, which will appear like an “M” tab. Expand it like project > Lifecycle > and then double-click on the package.

Creating a JAR Using the Spring Boot Maven Plugin-2

Step 4: Find the Output JAR

Go to the target folder inside your project, inside which you will see a JAR like demo-0.0.1-SNAPSHOT.jar

Creating a JAR Using the Spring Boot Maven Plugin-3

Step 5: Run the JAR

Open a terminal and run:

java -jar demo-0.0.1-SNAPSHOT.jar

Output:

Creating a JAR Using the Spring Boot Maven Plugin-4
Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

Create a Web Application With Executable Tomcat

The following steps should be followed to build a jar file using IntelliJ.

Step 1: Create or Open a Maven Project in IntelliJ

  • 1. Open IntelliJ IDEA
  • 2. Create New Spring-Boot Project            
Create a Web Application With Executable Tomcat-1

Now you will be able to see the pom.xml file and a src/main/java/com.example.demo structure.

Step 2: Write Your Main Class

Create a simple Java class under src/main/java/com/example/Main.java

package org.example.intellipaat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class IntellipaatApplication {
   public static void main(String[] args) {
       SpringApplication.run(IntellipaatApplication.class, args);
       System.out.println("Hello from Intellipaat");
   }
}

Create one more file RestController.java to get the output in the web browser by the @GetMapping(“/Intellipaat”) method .

package org.example.intellipaat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Rest {
   @GetMapping("/Intellipaat")
   public String hello() {
       return "Hello from Spring Boot with Intellipaat";
   }
}

Step 3: Build the Project in IntelliJ

Open the Maven window on the right sidebar, which will appear like an “M” tab. Expand it like project > Lifecycle > and then double-click on the package.

Create a Web Application With Executable Tomcat-2

Step 4: Find the Output JAR

Go to the target folder inside your project, where you will see a JAR like

Intellipaat-0.0.1-SNAPSHOT.jar

Create a Web Application With Executable Tomcat-3

Step 5: Run the JAR

Open a terminal and run:

java -jar Intellipaat-0.0.1-SNAPSHOT.jar

Output:                                                                      

Create a Web Application With Executable Tomcat-4

Step 6: Visit the App

Open your browser and go to:

http://localhost:8080/Intellipaat
Create a Web Application With Executable Tomcat-5

Convert JAR to EXE Using Launch4j in Maven

The following steps should be followed to convert JAR to EXE using Launch4J with Maven. Also, we will understand how to convert a Java JAR to Windows EXE.

Step 1: Create or Open a Maven Project in IntelliJ

  • 1. Open IntelliJ IDEA
  • 2. Create New Project           
Create an Executable Using Launch4j with Maven

Now you will be able to see the pom.xml file and a src/main/java structure.

Step 2: Write Your Main Class

Create a simple Java class under src/main/java/com/example/Main.java

package org.example.intellipaat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Intellipaat2Application {
   public static void main(String[] args) {
       SpringApplication.run(Intellipaat2Application.class, args);
       System.out.println("Hello from EXE! by Intellipaat");
   }
}

Step 3: Edit your pom.xml and add the following inside the <build><plugins> section

<build>
    <plugins>
        <!-- Spring Boot Plugin -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Launch4j Plugin for EXE generation -->
        <plugin>
            <groupId>com.akathist.maven.plugins.launch4j</groupId>
            <artifactId>launch4j-maven-plugin</artifactId>
            <version>1.7.21</version>
            <executions>
                <execution>
                    <id>l4j-config</id>
                    <phase>package</phase>
                    <goals>
                        <goal>launch4j</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
             <jar>${project.build.directory}/${project.build.finalName}.jar</jar>
                <outfile>${project.build.directory}/Intellipaat.exe</outfile>
                <dontWrapJar>false</dontWrapJar>
                <headerType>console</headerType>
                <errTitle>App Error</errTitle>
                <chdir>.</chdir>
                <priority>normal</priority>
                <cmdLine>--add-opens java.base/java.util=ALL-UNNAMED</cmdLine>
                <mainClass>org.example.intellipaat.Intellipaat2Application</mainClass>
                <classPath>
                    <mainClass>${project.build.finalName}.jar</mainClass>
                </classPath>
                <jre>
                    <path>src/main/launch4j/jre</path>
                    <minVersion>17</minVersion>
                    <bundledJre64Bit>true</bundledJre64Bit>
                    <jdkPreference>preferJre</jdkPreference>
                </jre>
            </configuration>
        </plugin>
    </plugins>
</build>

Step 4: Build the Project in IntelliJ

Open the Maven window on the right sidebar, which will appear like an “M” tab. Expand it like project > Lifecycle > and then double-click on the package

Create an Executable Using Launch4J in Maven-2

Step 5: Add the Launch4j Plugin to pom.xml

  • Open the Launch4J
  • Click on Browse and select your .jar file.
  • Choose the Output file name, e.g., Intellipaat.exe. This demonstrates converting a Java JAR to Windows EXE.
  • In the JRE tab, set the Minimum JRE version (e.g., 1.8 for Java 8 or later).
  • Click on Save Configuration to keep the settings for future use.
Create an Executable Using Launch4J in Maven-3
  • Generate the Executable (.exe) by clicking on Build Wrapper.
  • Launch4J will create an EXE file in the chosen output directory. This is the final step in the Java JAR to Windows EXE conversion.
Create an Executable Using Launch4J in Maven-4

Step 5: Run the .exe

Go to the target/ directory. Run the EXE.

./your-app-name.exe

You can also double-click it in Windows File Explorer.

Output:

Convert JAR to EXE Using Launch4J in Maven-Output

Pros and Cons: JAR vs EXE Creation Tools in Java

Method Pros Cons
Maven Assembly Plugin (JAR) Simple to set up and supports creating a fat JAR with all dependencies Can result in large file sizes and has issues with large dependency management
Maven Shade Plugin (JAR) Creates a single executable JAR with dependencies and supports advanced features like merging service files More complex compared to the Assembly Plugin
Spring Boot Maven Plugin (JAR) Automatically handles dependencies and has easy setup for Spring Boot applications Not designed for generic Java projects and requires Spring Boot configurations
Web Application with Executable Tomcat (JAR) Good for web applications and automatically integrates with Spring Boot Limited to web applications and requires Tomcat for deployment
Launch4j (EXE) Converts JAR to EXE for Windows with customizations (icon, error handling) Requires external tool (Launch4j) and limited to Windows
Creating EXE using Launch4j with Maven Plugin Automates EXE creation via Maven and easy to add to builds Windows-specific and not portable to other OS without additional steps

To know more about this topic, you can refer to our Java Course.

Conclusion

To generate .exe files, you’ll need to install and configure Launch4j or use Launch4j with Maven plugin to automate the process. Run the Maven clean and package command, and the jar file will be created. For the .exe files, you need to install Launch4J, and then you can produce the .exe file. Double-click on it to run it, or you can right-click on it and run it as an administrator.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Create an Executable JAR with Maven? – FAQs

Q1. How to create an executable Maven jar with dependencies?

To create a Maven jar with dependencies you can run the mvn package command.

Q2. What is the difference between mvn clean install and mvn clean package?

The maven clean command builds the project and installs the JARs, whereas the maven package command only builds the JAR.

Q3. How do you create a dependency in Maven?

To add a dependency in the Maven, edit the pom. xml file, and under the build or plugin or dependencies tags, you can add the dependency you need.

Q4. What is the difference between a jar and an executable Maven jar with dependencies?

The jar file contains the files from your projects, while the Maven executable jar has all the dependencies required to run a project, i.e., the JRE, source to the main class path.

Q5. What is the file extension for a Java executable?

The file extension for a Java executable is .jar.

Q6. Can I run an EXE created from a JAR on Mac/Linux?

No, an EXE created from a JAR won’t run on Mac/Linux because EXE files are Windows-specific executables.

Q7. Is Launch4J the only tool to convert JAR to EXE?

No, Launch4J is not the only tool to convert JAR to EXE; other options include JSmooth, JWrapper, and Excelsior JET.

Q8. How to convert Java JAR to Windows EXE?

You can convert a Java JAR to Windows EXE using tools like Launch4J or JSmooth.

Q9. How to create a runnable JAR using Maven?

You can create a runnable JAR using Maven by configuring the `maven-jar-plugin` or `maven-assembly-plugin` with the `mainClass` entry in the `pom.xml`.

Q10. How to run an executable JAR from the terminal?

You can run an executable JAR from the terminal using the command: java -jar filename.jar.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner