Running Executable JAR Files in Java
JAR (Java Archive) files are commonly used to package Java code, resources, and metadata for distribution and execution. While JAR files can be used as libraries, they can also be packaged as executable JARs, meaning they contain a main
method that serves as the entry point for the application. This tutorial explains how to create and run executable JAR files.
Understanding Executable JARs
An executable JAR file is distinct from a regular JAR file used solely as a library. To make a JAR executable, the JAR file needs to specify which class contains the main
method that should be executed when the JAR is run. This information is stored within the JAR’s manifest file.
The Manifest File
The manifest file (MANIFEST.MF
) is a special file within the JAR archive that contains metadata about the JAR. To make a JAR executable, the manifest must include a Main-Class
attribute, which specifies the fully qualified name of the class containing the main
method.
Here’s an example of a minimal MANIFEST.MF
file:
Manifest-Version: 1.0
Main-Class: com.example.MyMainClass
The Main-Class
attribute must point to a valid class with a public static void main(String[] args)
method.
Creating an Executable JAR
Let’s walk through the process of creating an executable JAR file.
1. Java Code:
First, create a simple Java program. For example:
package com.example;
public class MyMainClass {
public static void main(String[] args) {
System.out.println("Hello, executable JAR!");
}
}
2. Compiling the Code:
Compile the Java code using javac
:
javac com/example/MyMainClass.java
3. Creating the Manifest File:
Create a text file named manifest.txt
(or any name you prefer) with the following content:
Manifest-Version: 1.0
Main-Class: com.example.MyMainClass
Important: Ensure the last line of the manifest.txt
file ends with a newline character. Some tools may not parse the file correctly if it doesn’t.
4. Creating the JAR File:
Use the jar
command to create the JAR file. The jar cfm
command is crucial here:
c
: Creates a new JAR file.f
: Specifies the name of the JAR file.m
: Includes the manifest file.
The command would be:
jar cfm MyExample.jar manifest.txt com/example/MyMainClass.class
This creates a JAR file named MyExample.jar
that includes the manifest file and the compiled class.
Running the JAR File
Once the JAR file is created, you can run it using the following command:
java -jar MyExample.jar
This command instructs the Java runtime to execute the JAR file, using the Main-Class
specified in the manifest file as the entry point. The output of the program (in this case, "Hello, executable JAR!") will be printed to the console.
Alternative: Using Build Tools
For more complex projects, using a build tool like Maven or Gradle is highly recommended. These tools automate the process of compiling code, managing dependencies, and creating JAR files, including setting the Main-Class
attribute in the manifest.
Troubleshooting
Failed to load Main Class
: This error usually indicates that theMain-Class
attribute is either missing from the manifest or is incorrectly specified. Double-check the manifest file and ensure that the class name is fully qualified and correct.- JAR file runs but does not behave as expected: Verify that all necessary resources (images, configuration files, etc.) are included in the JAR file and that your code correctly accesses them.
- Classpath issues: If your application depends on external libraries, ensure that they are included in the classpath when running the JAR file. Build tools typically handle classpath management automatically.