Preface
In Java development, packaging JAR (Java ARchive) files are compiled.class
Files, resource files (such as configuration files, pictures, etc.) and dependent library files are packaged into a compressed file for common operations that are easy to distribute and run. This article will explain in detail how to type Java files into separate JAR packages, including detailed instructions for each step (including maven projects and non-maven projects).
1. How to separately form JAR packages for non-Maven projects
1. Preparation
Before you start packaging, make sure you already have the following environments and files:
-
Java Development Kit (JDK): Make sure that the JDK is installed and the environment variables are configured correctly (
JAVA_HOME
)。 -
Java source code: Java programs that have been written and compiled, usually stored in
src
In the directory. -
Manifest file (optional): Metadata file used to specify main class and other JAR files (
)。
Suppose you have a simple Java project structure as follows:
MyProject/
│
├── src/
│ └── com/
│ └── example/
│ └──
│
└── manifest/
└──
in,It is your main program file.
is your optional manifest file.
2. Compile Java files
First, you need to.java
Compiled source file into.class
Bytecode file. Suppose you are currently in the root directory of the project (i.e.MyProject/
under the directory).
Order:
javac -d out/production/classes src/com/example/
-
-d
The parameters specify the compiled.class
File storage directory. -
out/production/classes
It is after compilation.class
The output directory of the file.
After execution, you will get the following project structure:
MyProject/
│
├── out/
│ └── production/
│ └── classes/
│ └── com/
│ └── example/
│ └──
│
├── src/
│ └── com/
│ └── example/
│ └──
│
└── manifest/
└──
3. Create a Manifest file (optional)
The Manifest file can specify the entry class and other metadata of the JAR package. If the manifest file is not provided, the JAR package will not be run directly from the command line.
Example File content:
Manifest-Version: 1.0 Main-Class:
-
Main-Class
Specifies the entry class of the JAR package, that ispublic static void main(String[] args)
The class where the method is located.
4. Package into JAR files
After the compilation is complete and the (optional) manifest file is ready, you can usejar
The command packages the project into a JAR file.
Order:
jar cvfm manifest/ -C out/production/classes/ .
-
c
Options indicate the creation of a new JAR file. -
v
Options indicate the generation of detailed output. -
f
Options represent the name of the specified JAR file. -
m
Options indicate the addition of the manifest file to the JAR file. -
-C
Options indicate that you switch to the specified directory and add files under that directory to the JAR file. -
is the name of the generated JAR file.
After execution, you will get the following project structure:
MyProject/
│
├──
│
├── out/
│ └── production/
│ └── classes/
│ └── com/
│ └── example/
│ └──
│
├── src/
│ └── com/
│ └── example/
│ └──
│
└── manifest/
└──
It is the JAR file you packed.
5. Run the JAR file
After generating the JAR file, you can run it with the following command:
Order:
java -jar
If the JAR package contains the correct oneMain-Class
Information, it will directly execute the main program.
6. FAQs and Solutions
6.1 The main class cannot be found
If it appears when running the JAR packageError: Could not find or load main class
Errors are usually becauseMain-Class
Incorrect configuration or path error. make surein the file
Main-Class
Point to the correct class and used when packaging-m
Options include the manifest file in the JAR package.
6.2 Missing dependency library after packaging
If your project depends on external libraries, you need to package these libraries into a JAR file. Can be usedfat JAR
oruber JAR
method to package all dependency libraries into a JAR file. Common tools such as Maven'sshade
Plugin or Gradle'sshadow
Plugins can achieve this.
2. How to make a JAR package for Maven project separately
When using Maven for project management, packaging projects into JAR files is a very common operation. Maven greatly simplifies the compilation and packaging process of code by providing built-in lifecycle phases. Next, we will explain in detail how to use Maven to package an e-commerce system project into a JAR file separately.
2.1 Configuration File
First, make sure yoursThe relevant dependencies and plug-ins have been correctly configured in the file. Usually, we need to use Maven's
maven-jar-plugin
Let's help us complete the packaging operation.
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>ecommerce-system</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <!-- Add the dependencies required for the project here --> </dependencies> <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>
- artifactId: A unique identifier for a project, usually consistent with the project name.
-
version: The version number of the project, usually
SNAPSHOT
(In-development version) or a specific release version number. -
packaging: Specify the packaging type as
jar
。 -
mainClass: Specify the main classpath of the project, this class contains
main
Method is the entry to the program.
2.2 Packaging using the Maven command
Configure wellAfter the file, you can use the Maven command to package the project.
mvn clean package
After executing this command, Maven will perform the following steps:
- Cleaning phase: Delete the contents in the target directory generated by the previous compile.
- Compilation stage: Compile the source code and compile the Java file into a bytecode file (.class).
- Testing phase: Execute test cases to ensure that the code is functional (if there are test cases).
- Packaging stage: Package the compiled files and resource files into a JAR file.
After the package is successful, the JAR file will be generated intarget
In the directory, the file name is usually,For example
ecommerce-system-1.
。
2.3 Run the generated JAR package
After packaging, you can run the generated JAR file through the following command:
java -jar target/ecommerce-system-1.
ifThe file is configured correctly and the program will start smoothly.
2.4 Merge dependencies using maven-shade-plugin
Sometimes, we may need to package all dependencies into a JAR file together, which can be usedmaven-shade-plugin
Plugin.
existAdd the following configuration:
<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> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> </execution> </executions> </plugin> </plugins> </build>
Then execute againmvn clean package
The generated JAR file will contain all dependencies for easy direct operation.
In this way, you will successfully package the Maven project into an executable JAR file, and you can continue to deploy and run the JAR file in depth.
Summarize
This is the article about making Java files into independent JAR packages. For more related Java files into independent JAR packages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!