In this post, we’ll learn how to create an executable JAR file that contains all the dependencies required to run a Java application using Maven. Maven is a popular build tool that can simplify the process of creating JAR files with dependencies.
What is an executable JAR?
An executable JAR is a standard Java archive file that can be run directly without the need to extract its contents. It contains the compiled classes, resources, and dependencies of a Java application, making it easy to distribute and run the application on any system with a JRE installed.
Prerequisites:
- Maven installed on your system
- A Java project with a pom.xml file
Step 1: Define the Main Class
To create an executable JAR, you must specify the main class of your application. This class will be the entry point for the application and will be executed when the JAR file is run.
To specify the main class, add the following code to your pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
Replace com.example.MainClass
with the fully-qualified name of your main class.
Step 2: Package Dependencies
To include dependencies in your JAR file, you must add the following code to your pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <finalName>executable-jar</finalName> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Step 3: Build the JAR
Finally, run the following command to build the JAR file:
mvn clean package
This command will compile your application, package it into a JAR file, and include all the required dependencies.
The JAR file will be located in the target
directory of your project and can be run using the following command:
java -jar executable-jar.jar
Step 4: Include External Dependencies
If your application relies on external dependencies that are not available in the Maven Central repository, you can include them in the JAR file by specifying the path to the dependency in the pom.xml file.
For example, to include a local JAR file, add the following code to your pom.xml file:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/dependency.jar</systemPath> </dependency> </dependencies>
Replace com.example
, dependency
, 1.0
, and ${project.basedir}/lib/dependency.jar
with the relevant information for your external dependency.
Step 5: Exclude Unnecessary Dependencies
By default, Maven will include all transitive dependencies in the JAR file. However, if you have any unnecessary dependencies, you can exclude them to reduce the size of the JAR file.
To exclude a dependency, add the following code to your pom.xml file:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>com.unnecessary</groupId> <artifactId>unnecessary-dependency</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
Replace com.example
, dependency
, 1.0
, com.unnecessary
, and unnecessary-dependency
with the relevant information for the dependency that you want to exclude.
Troubleshooting
If you encounter any errors while building the JAR file, check the following:
- Make sure that the main class specified in the pom.xml file is correct.
- Make sure that all dependencies are properly specified in the pom.xml file.
- Make sure that all external dependencies are available in the specified path.
Conclusion
In conclusion, creating an executable JAR with dependencies using Maven can be a simple and efficient process. By following the steps outlined in this post, you can easily build and distribute your Java application with all the necessary dependencies.