# How to Fix Java JAR File Not Executable Error
You created a JAR file, but when you try to run it, you get one of these errors:
no main manifest attribute, in myapp.jarOr:
Error: Could not find or load main class com.myapp.MainOr:
Exception in thread "main" java.lang.NoClassDefFoundError: com/myapp/dependency/Logger
at com.myapp.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.myapp.dependency.LoggerThese errors occur when the JAR lacks proper manifest configuration or is missing dependencies.
Understanding JAR Execution
- 1.For a JAR to be executable with
java -jar myapp.jar, it needs: - 2.A
MANIFEST.MFfile inMETA-INF/ - 3.A
Main-Classattribute pointing to your main class - 4.All dependencies either bundled or on the classpath
Diagnosis Steps
Step 1: Check for Main-Class Attribute
```bash # Extract and view manifest jar tf myapp.jar | grep MANIFEST # Output: META-INF/MANIFEST.MF
# View manifest contents unzip -p myapp.jar META-INF/MANIFEST.MF
# Or extract and view jar xf myapp.jar META-INF/MANIFEST.MF cat META-INF/MANIFEST.MF ```
A valid manifest for executable JAR should look like:
Manifest-Version: 1.0
Main-Class: com.myapp.MainStep 2: Verify Main Class Exists in JAR
```bash # List all classes in JAR jar tf myapp.jar | grep Main
# Check if class file exists jar tf myapp.jar | grep "com/myapp/Main.class" ```
Step 3: Check for Dependencies
```bash # See if JAR contains dependencies jar tf myapp.jar | head -50
# If you see only your classes, dependencies are missing # If you see other package structures, dependencies may be bundled ```
Step 4: Test Class Execution
```bash # Try running main class directly (not as JAR) java -cp myapp.jar:dependency1.jar:dependency2.jar com.myapp.Main
# If this works, your manifest or dependencies are the issue ```
Solutions
Solution 1: Create Manifest Manually
Create a manifest file:
```bash # Create manifest file cat > MANIFEST.MF << 'EOF' Manifest-Version: 1.0 Main-Class: com.myapp.Main
EOF # Note: manifest must end with a blank line ```
Create JAR with manifest:
```bash # Compile your code javac -d out src/com/myapp/Main.java
# Create JAR with manifest jar cfm myapp.jar MANIFEST.MF -C out . ```
Solution 2: Create Executable JAR with jar Command
```bash # Compile with output directory javac -d build/classes src/com/myapp/*.java
# Create JAR with Main-Class jar cfe myapp.jar com.myapp.Main -C build/classes .
# Run java -jar myapp.jar ```
The -e flag specifies the Main-Class directly.
Solution 3: Configure Maven for Executable JAR
Using maven-jar-plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.myapp.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>This creates a JAR with manifest pointing to a lib/ folder of dependencies.
Using maven-shade-plugin (Fat JAR):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.myapp.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>Build and run:
mvn clean package
java -jar target/myapp-1.0.0.jarUsing maven-assembly-plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.myapp.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>Solution 4: Configure Gradle for Executable JAR
Using application plugin:
```groovy plugins { id 'application' }
application { mainClass = 'com.myapp.Main' }
// Run with: ./gradlew run // Create distribution: ./gradlew installDist ```
Using shadow plugin (Fat JAR):
```groovy plugins { id 'java' id 'com.github.johnrengelman.shadow' version '8.1.1' }
group = 'com.myapp' version = '1.0.0'
repositories { mavenCentral() }
dependencies { implementation 'org.slf4j:slf4j-api:2.0.9' implementation 'ch.qos.logback:logback-classic:1.4.11' }
jar { manifest { attributes( 'Main-Class': 'com.myapp.Main' ) } }
shadowJar { archiveBaseName.set('myapp') archiveClassifier.set('') archiveVersion.set('') } ```
Build and run:
./gradlew shadowJar
java -jar build/libs/myapp.jarSolution 5: Fix Missing Dependencies
If you have a "thin" JAR without dependencies:
```bash # Option 1: Run with classpath java -cp "myapp.jar:lib/*" com.myapp.Main
# Option 2: Use manifest-only JAR with Class-Path cat > MANIFEST.MF << 'EOF' Manifest-Version: 1.0 Main-Class: com.myapp.Main Class-Path: lib/dependency1.jar lib/dependency2.jar
EOF
jar cfm myapp.jar MANIFEST.MF -C out . ```
Solution 6: Fix Main-Class Not Found
If the class exists but isn't found:
```java // Ensure main method signature is correct package com.myapp;
public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ```
Check the compiled class:
```bash # Verify class has main method javap -public build/classes/com/myapp/Main.class
# Should show: # public static void main(java.lang.String[]); ```
Solution 7: Create Modular JAR (Java 9+)
For modular applications:
// module-info.java
module com.myapp {
exports com.myapp;
requires org.slf4j;
}```bash # Compile module javac -d out --module-source-path src $(find src -name "*.java")
# Create modular JAR jar --create --file myapp.jar --main-class com.myapp.Main -C out .
# Run java -jar myapp.jar # Or with module path java -p myapp.jar -m com.myapp/com.myapp.Main ```
Verification
After building, verify the JAR:
```bash # Check manifest unzip -p myapp.jar META-INF/MANIFEST.MF
# Verify Main-Class grep "Main-Class" META-INF/MANIFEST.MF
# Test run java -jar myapp.jar
# If fails, check classpath java -verbose:class -jar myapp.jar 2>&1 | head -20 ```
Create a test script:
```bash #!/bin/bash # test-jar.sh
JAR_FILE=$1
echo "=== Checking JAR: $JAR_FILE ==="
# Check if file exists if [ ! -f "$JAR_FILE" ]; then echo "ERROR: JAR file not found" exit 1 fi
# Check manifest echo -e "\n=== Manifest ===" unzip -p "$JAR_FILE" META-INF/MANIFEST.MF
# Check main class echo -e "\n=== Main Class ===" MAIN_CLASS=$(unzip -p "$JAR_FILE" META-INF/MANIFEST.MF | grep "Main-Class:" | cut -d: -f2 | tr -d ' ') if [ -n "$MAIN_CLASS" ]; then echo "Main-Class: $MAIN_CLASS" CLASS_FILE=$(echo "$MAIN_CLASS" | tr '.' '/').class if unzip -l "$JAR_FILE" | grep -q "$CLASS_FILE"; then echo "✓ Main class found in JAR" else echo "✗ Main class NOT found in JAR" fi else echo "✗ No Main-Class attribute in manifest" fi
# Try running echo -e "\n=== Running JAR ===" java -jar "$JAR_FILE" ```
Quick Reference
| Error | Cause | Solution |
|---|---|---|
| no main manifest attribute | No Main-Class in MANIFEST.MF | Add Main-Class to manifest |
| Could not find main class | Wrong class name or missing class | Check class name and file existence |
| NoClassDefFoundError | Dependencies not in JAR | Use shade/assembly plugin or specify classpath |
| ClassNotFoundException | Class missing from JAR | Rebuild and verify compilation |
| Invalid or corrupt jarfile | Malformed JAR | Rebuild JAR |
The simplest path to an executable JAR: use Maven shade plugin or Gradle shadow plugin to create a "fat JAR" with all dependencies bundled.