introduction
JAR files are the standard format for packaging and distribution of Java applications. In particular, executable JAR files can package all the necessary components of a Java application (including class files, resource files, and dependency libraries) together, making it easier to distribute and run applications. In this article, we will detail the various methods and best practices for creating, configuring, and using executable JAR files.
Introduction to JAR file
A JAR file is an archive file based on the ZIP file format. It can contain multiple Java class files, metadata and resource files. JAR files can be used for:
- Package Java class files and related resources.
- Distribute Java applications or libraries.
- Improve download speed and reduce storage space.
The main advantage of JAR files is that they can package multiple files into an archive file, simplifying the distribution and deployment of applications.
Create a JAR file
JDK's jar tool
JDK provides a command line tooljar
, can be used to create and manipulate JAR files. Here are the basic steps to create a JAR file:
Compile Java source files:
javac
Create a JAR file:
jar cvf
Herec
Indicates creating a new JAR file.v
Indicates the generation of detailed output,f
Indicates the specified JAR file name.
Create a JAR file using IDE
Most IDEs, such as Eclipse and IntelliJ IDEA, provide graphical interface tools for creating JAR files. Here are the steps to create a JAR file using Eclipse:
- Select the project and right-click it, select
Export...
。 - Select in the pop-up dialog box
Java
->JAR file
。 - Specify the output path and file name, click
Finish
。
Specify the Main-Class property
In order to make the JAR file executable, we need toSpecify the main class in the file (including
main
class of method).
Specify in the file
A file is a special file in a JAR file that contains the metadata of the JAR file. Here is an example
document:
Manifest-Version: 1.0 Main-Class:
Use the jar tool to specify
We can use it when creating a JAR filejar
Toolse
Options specify the main class:
jar cvfe
Heree
Options are used to specify the main class.
Run an executable JAR file
After creating the executable JAR file, you can run it with the following command:
java -jar
This command starts the main class specified in the JAR file and executes itmain
method.
Create an executable JAR file in a Maven project
Configuration
In the Maven project, we can configure itFile to create an executable JAR file.
Using maven-jar-plugin
Here is a configuration example:
<project> ... <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass></mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
After configuration, run the following command to generate a JAR file:
mvn package
Create an executable JAR file in a Gradle project
Configuration
In the Gradle project, we can configure itFile to create an executable JAR file.
Using the application plugin
Here is a configuration example:
plugins { id 'java' id 'application' } mainClassName = '' jar { manifest { attributes( 'Main-Class': mainClassName ) } }
After configuration, run the following command to generate a JAR file:
gradle build
Executable JAR file containing dependent libraries
Executable JAR files usually need to contain the application's dependency library. We can use the Maven Shade plugin or the Gradle Shadow plugin to create an executable JAR file containing all dependencies.
Using the Maven Shade plugin
Here is an example configuration of a Maven Shade plugin:
<project> ... <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation=""> <mainClass></mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
After configuration, run the following command to generate an executable JAR file containing all dependencies:
mvn package
Using Gradle Shadow Plugin
Here is an example configuration of a Gradle Shadow plugin:
plugins { id 'java' id 'application' id '' version '6.1.0' } mainClassName = '' shadowJar { manifest { attributes( 'Main-Class': mainClassName ) } }
After configuration, run the following command to generate an executable JAR file containing all dependencies:
gradle shadowJar
Executable JAR files in multi-module projects
In a multi-module project, we need to ensure that all module class files and resource files are included in the executable JAR file. Here is an example configuration for a multi-module Maven project:
<project> ... <modules> <module>module1</module> <module>module2</module> </modules> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass></mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
In a multi-module project, make sure all submodules are packaged and included in the final executable JAR file.
Summarize
This article introduces in detail the creation, structure and usage of Java executable JAR files. We discussed how to use JDKjar
Tools, IDE, Maven, and Gradle to create and configure executable JAR files and how to include dependency libraries in JAR files. By mastering these technologies, you can package and distribute Java applications more efficiently, simplify deployment processes, and improve development efficiency.
This is the end of this article about various ways to create executable JAR files in Java. For more related contents of Java creation executable JAR files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!