how to fix no main manifest attribute error when running Java JAR

By Steve Claridge on 2022-11-09.

You are building a Java application, let's say a vanilla command-line app, and using Gradle as your build mechanism. After building your run it with something like

java -jar yourjar-0.1.jar

and you get the following error:

no main manifest attribute, in yourjar-0.1.jar

This is because, be default, the build.gradle config doesn't add the manifest to the JAR to make it executable. This is the case even if you create a Gradle Project in IntelliJ.

It's easy to fix though, add the following to your build.gradle file:

jar {
    manifest {
        attributes(
            'Main-Class': 'yourpackage.Main'
        )
    }
}

If you copy/paste this, don't forget to change yourpackage.Main to your intended Main class.

You might also want to check out Creating a fat JAR with Gradle.