Of course! Creating a "Runnable JAR" in Java is a fundamental skill for any developer. It allows you to package your application, along with all its dependencies, into a single file that can be easily distributed and run on any machine with a Java Runtime Environment (JRE).

Let's break down everything you need to know, from the basics to the different ways to create one and how to handle common issues.
What is a Runnable JAR?
A Runnable JAR (or Executable JAR) is a special type of Java Archive (.jar) file that includes:
- Your compiled
.classfiles. - All required libraries (dependencies).
- A special manifest file (
META-INF/MANIFEST.MF) that tells the Java Virtual Machine (JVM) which class contains thepublic static void main(String[] args)method to run as the application's entry point.
Think of it as a self-contained box for your application.
The Two Main Ways to Create a Runnable JAR
There are two primary methods for creating a runnable JAR:

- Using an IDE (like IntelliJ IDEA or Eclipse): The most common and user-friendly method, especially for beginners.
- Using a Build Tool (like Maven or Gradle): The professional, automated, and highly recommended method for any non-trivial project.
Method 1: Using an IDE (Example with IntelliJ IDEA)
This is the simplest way for small projects.
Step-by-Step Guide:
-
Open your Project: In IntelliJ IDEA, open your existing Java project.
-
Open "Build Artifacts" Configuration:
- Go to
File->Project Structure...(or pressCtrl+Alt+Shift+S). - In the left-hand menu, select
Artifacts. - Click the (Add) button in the top-left corner.
- Choose
JAR->From modules with dependencies....
- Go to
-
Configure the JAR:
(图片来源网络,侵删)- Main Class: This is the most important part. Click the button, browse your project, and select the class that contains your
mainmethod. - Directory for META-INF/MANIFEST.MF: You can leave this as
META-INF(default). - Build on Make: Check this box if you want the JAR to be rebuilt automatically every time you build your project.
- Extract to the target JAR: This is a crucial choice.
- Checked (Recommended): All your library dependencies will be extracted from their separate JARs and placed directly into your main JAR. This results in a single file, which is easy to distribute. However, it can increase the JAR's size slightly and may cause issues if libraries rely on their own internal file structures.
- Unchecked: Your main JAR will reference the library JARs. You will need to include all library JARs in the same directory when you distribute your application. This keeps the main JAR smaller but requires managing multiple files.
- Main Class: This is the most important part. Click the button, browse your project, and select the class that contains your
-
Apply and OK: Click
Applyand thenOKto save the artifact configuration. -
Build the JAR:
- Go to
Build->Build Artifacts.... - Select your artifact from the list (e.g.,
your-project-name:jar). - Click
Build.
- Go to
-
Find Your JAR: The built JAR file will be located in a directory like
out/artifacts/your-project_name_jar/.
Method 2: Using a Build Tool (Example with Maven)
Maven is the industry standard for project management and build automation in Java. It handles dependencies and packaging for you.
Step-by-Step Guide:
-
Create a
pom.xml: If you don't have one, create a standard Maven project structure (src/main/java,src/main/resources,pom.xml). -
Configure the
maven-jar-plugin: This is the plugin that tells Maven how to create your JAR. You need to specify the main class in the manifest.<project ...> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <!-- Use a recent version --> <configuration> <!-- This is the crucial part! --> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.example.myapp.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ... </project><mainClass>: Replacecom.example.myapp.Mainwith your actual main class's fully qualified name.<addClasspath>true</addClasspath>: This tells the plugin to automatically add aClass-Pathentry to the manifest, listing all your dependencies.
-
Build the Project: Open a terminal in your project's root directory and run the package goal.
mvn clean package
-
Find Your JAR: Maven will create a
targetdirectory. Inside, you will find your JAR file, typically named something likemy-app-1.0-SNAPSHOT.jar. You will also find a-javadoc.jarand a-sources.jar, which you can ignore.
Note on Maven "Fat JAR" (All-in-One):
The above method creates a "thin" JAR that depends on other JARs. For a single, distributable file, you need a "fat JAR" or "uber JAR". Use the maven-shade-plugin or maven-assembly-plugin.
Here's a simple example with the maven-assembly-plugin:
-
Add the Plugin to
pom.xml:<project ...> ... <build> <plugins> <!-- ... other plugins like maven-jar-plugin ... --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.example.myapp.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project> -
Build Again:
mvn clean package
-
Find Your Fat JAR: In the
targetdirectory, you will now findmy-app-1.0-SNAPSHOT-jar-with-dependencies.jar. This is the single file you can distribute.
How to Run a Runnable JAR
Once you have your JAR file, running it is simple.
From the Command Line
-
Open a terminal or command prompt.
-
Navigate to the directory containing your JAR file.
-
Use the
javacommand with the-jarflag.java -jar your-jar-file.jar
For example:
java -jar my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
On Windows (Double-Click)
You can often just double-click the JAR file. However, this can be unreliable because it depends on the file association for .jar files, which might not be set correctly or might not include the necessary classpath for dependencies. The command line method is always the most reliable.
Common Problems and Solutions
Problem: Error: Could not find or load main class
This is the most common error. It means the JVM couldn't locate the class you specified in the manifest.
Causes and Solutions:
-
Incorrect Main Class in Manifest: The fully qualified class name in the manifest might be wrong.
- Solution: Double-check that
<mainClass>in yourpom.xmlor IDE artifact settings is correct (e.g.,com.example.Main, not justMain).
- Solution: Double-check that
-
Classpath Issues: If you are not using a fat JAR, the dependencies might not be on the classpath.
- Solution (for thin JARs): When running, you must provide the classpath manually.
java -cp "your-jar-file.jar;lib/*" com.example.Main
(Note the on Windows and on Linux/macOS, and the to include all JARs in the
libdirectory).
- Solution (for thin JARs): When running, you must provide the classpath manually.
-
JAR is Corrupted or Not Runnable: You might be trying to run a regular library JAR.
- Solution: Ensure the JAR was built correctly as a "Runnable" or "Executable" JAR with a
Main-Classmanifest entry. You can inspect the manifest using:jar tf your-jar-file.jar | grep META-INF/MANIFEST.MF
And then view its contents:
jar xf your-jar-file.jar META-INF/MANIFEST.MF cat META-INF/MANIFEST.MF
- Solution: Ensure the JAR was built correctly as a "Runnable" or "Executable" JAR with a
Problem: NoClassDefFoundError or ClassNotFoundException
This error means the JVM found your main class but couldn't find one of its dependencies.
- Cause: The required library is not on the classpath.
- Solution:
- If you used a fat JAR, this should not happen. The build process likely failed to include the dependency. Re-run your build.
- If you used a thin JAR, ensure all required JAR files are in the same directory or are explicitly included in the
-cpargument when you run the application.
