An executable JAR (Java Archive) file lets you run a Java application with a single click or command. Unlike standard JARs, executable JARs include a main class defined in the MANIFEST.MF file, making them self-runnable. In this guide, you’ll learn three methods to create executable JARs—using the command line, Maven, and Eclipse IDE.
Table of Contents
Prerequisites
- Java Development Kit (JDK) installed (version 8 or later).
- Basic familiarity with Java programming.
Method 1: Create Executable JAR via Command Line
Step 1: Write a Java Program
Create a simple Java class with a main method. For example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Executable JAR!");
}
}
Save it as HelloWorld.java.
Step 2: Compile the Java File
Open Terminal/Command Prompt and compile the code:
javac HelloWorld.java
This generates HelloWorld.class.
Step 3: Create a Manifest File
The manifest defines the main class. Create a manifest.txt file with:
Main-Class: HelloWorld
Note: Add a newline at the end of the file to avoid errors.
Make sure to save both
HelloWorld.javaandmanifest.txtfile in same location/folder.
Step 4: Build the Executable JAR
Run the jar command to bundle files:
jar cfm HelloWorld.jar manifest.txt HelloWorld.class
c: Create a JAR.f: Specify the JAR filename.m: Include the manifest file.
Step 5: Run the Executable JAR
Test it with:
java -jar HelloWorld.jar
Output: Hello, Executable JAR!
Method 2: Create Executable JAR with Maven
Step 1: Configure the pom.xml File
Add the Maven JAR Plugin to your project’s pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.codersathi.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Replace com.example.HelloWorld with your main class’s full package path.
Step 2: Build the JAR
Run:
mvn clean package
The executable JAR will be generated in the target folder.
Method 3: Create Executable JAR in Eclipse
Step 1: Export the Project as a JAR
- Right-click your project > Export > Java > JAR File.
- Select resources to include and specify the JAR output path.
Step 2: Define the Main Class
- In the export wizard, click Next until you reach the JAR Manifest Specification screen.
- Check Generate the manifest file and specify your main class under Main Class.
Step 3: Finish and Run
Click Finish. Run the JAR via:
java -jar YourProject.jar
Demo Video
Troubleshooting Executable JAR Issues
1. “No main manifest attribute”
- Solution: Ensure the
Main-Classis correctly defined in the manifest.
2. “Could not find or load main class”
- Solution: Verify the main class’s package path matches the manifest entry.
Conclusion
You’ve now learned three ways to create an executable JAR file:
- Command Line: Ideal for small projects.
- Maven: Best for automated builds and dependency management.
- Eclipse: Perfect for IDE users.
For advanced use cases, explore creating fat JARs or bundling apps into native executables.
Need help? Check out our guide on how to run JAR files