How to fix “no main manifest attribute” error

Every executable jar file in a Java application should contain a main method. It is usually placed at the beginning of the application. Manifest files must be included with self-executing jars, as well as being wrapped in the project at the appropriate location, to run a main method. Manifest files have a main attribute that specifies the class having the main method.

"no main manifest attribute" is a common error which happens when we try to run an executable jar file. The full error output may look like what's shown below

Unable to execute jar- file: "no main manifest attribute." 

Or

no main manifest attribute, in target/exec.jar

img

In this article, we will show you a few possible fix that you can apply to your Java project to avoid getting "no main manifest attribute" error.

Why "no main manifest attribute"?

"no main manifest attribute" error message is thrown because of various reasons, but most of the time, it fall under one of the following category.

  • Missing entry point of Main-Class in MANIFEST.MF file.
  • Missing Maven dependency in pom.xml
  • Missing entry point in build.gradle

The Main, or Main-Class is an essential attribute to make your jar executable. It tells Java which class would be used as the entry point of the application.

Without a Main, or Main-Class attribute, Java have no way to know which class it should run when you execute the jar.

Inside the jar file, the MANIFEST.MF file is located in META-INF folder. Opening the jar file with WinRAR, you would see the contents of it, along with MANIFEST.MF.

A typical MANIFEST.MF file should contain the following lines

Manifest-Version: the version of the Manifest file.
Built-By: your PC name.
Build-Jdk: the JDK version installed in your machine.
Created-By: the plugin name used in IDE.

Below is an example of a MANIFEST.MF file.

Main-Class in MANIFEST.MF

Putting maven-jar-plugin in pom.xml

The "no main manifest attribute" error message may occur in a Maven project due to the absence of the Main-Class entry in MANIFEST.MF.

This issue can be resolved by adding maven-jar-plugin to our pom.xml file.

<build>  
    <plugins>  
        <plugin>  
            <!-- Build an executable JAR -->  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>3.1.0</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.linuxpip.AppMain</mainClass>  
                    </manifest>  
                </archive>  
            </configuration>  
        </plugin>  
    </plugins>  
</build>  

In the code snippet above, com.linuxpip.AppMain is our fully-qualified name of the Main-Class. You have to change this according to your specific project.

If you need more information, dig deep into maven-jar-plugin documentation: see https://maven.apache.org/plugins/maven-jar-plugin/

Specify Main-Class in Gradle

The following entries can be put into your build.gradle file if you receive this error in your Gradle project:

plugins {
    id 'java'
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.linuxpip.MyClass'
        )
    }
}

In the code snippet above, com.linuxpip.MyClass is our fully-qualified name of the Main-Class. You have to change this according to your specific project.

Change default MANIFEST.MF folder in IntelliJ IDEA

People have been reporting that IntelliJ IDEA keeps putting the JAR artifact into the wrong folder.

In order to fix "no main manifest attribute" in IntelliJ IDEA, choose JAR > From modules with dependencies

From modules with dependencies

In the Create JAR from Modules window, change the default Directory for META-INF/MANIFEST.MF path from <project folder>\src\main\java to <project folder>\src\main\resources.

Otherwise it would generate the manifest and including it in the jar, but not the one in <project folder>\src\main\java that Java expects.

After that, just continue to Build Artifacts as you usually do.

IntelliJ IDEA Build Artifacts

Specify entry point in Eclipse

If you're exporting the JAR file in Eclipse, it has a built-in option that allows you to specify the Application's entry point, avoiding "no main manifest attribute" error message.

In Eclipse's JAR Export window, you would see "Select the class of the application entry point" near the end of the Export process. Now you can pick a class and Eclipse will automatically generate the proper MANIFEST.MF file with the settings you've set.

Fix "no main manifest attribute" error message in Eclipse

Click to rate this post!
[Total: 18 Average: 5]

Leave a Comment