IntelliJ生成可执行API JAR时提示no main manifest attribute错误求助
Hey there, let's work through this frustrating issue together. Since you mentioned you can see your main class in the Artifact wizard, we can skip the basics and focus on the often-overlooked details that cause this error even when the main class exists.
1. Double-check your IntelliJ Artifact Manifest configuration
Sometimes the manifest is either missing or not properly linked to your main class. Here's how to verify:
- Open Project Structure (use
Ctrl+Alt+Shift+Son Windows/Linux,Cmd+;on Mac) - Navigate to Artifacts and select your
api.jarartifact - Look for a
META-INF/Manifest.mfentry under the artifact. If it's missing:- Right-click the artifact > Add > Empty Manifest
- Choose the path to store the manifest (typically
src/main/resources/META-INF/so it's included in your source files)
- Once the manifest exists, select it and in the right-hand pane, click the
...next to Main Class - Pick your main class from the list, save the configuration, and rebuild the artifact
2. Ensure your build tool explicitly sets the main class
If you're using Maven or Gradle, relying solely on IntelliJ's artifact wizard might not be enough—your build script needs to define the main class too:
For Maven users
Add or update the maven-jar-plugin in your pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <!-- Replace with your actual main class path --> <mainClass>com.yourcompany.api.YourMainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
Then run mvn clean package to rebuild the JAR.
For Gradle users
Add this block to your build.gradle (or build.gradle.kts for Kotlin):
jar { manifest { attributes 'Main-Class': 'com.yourcompany.api.YourMainClass' // Replace with your main class } }
Rebuild with ./gradlew clean build (or gradlew.bat clean build on Windows).
3. Verify the manifest inside the JAR
Let's confirm the manifest was actually included correctly:
- Unzip your
api.jar(useunzip api.jarin the terminal, or a GUI tool like WinZip) - Navigate to the
META-INFfolder and openManifest.mf - Look for a line that looks like:
Main-Class: com.yourcompany.api.YourMainClass
- Make sure there's one space after the colon, and this line isn't wrapped or missing. If it's not present, your build process isn't generating the manifest properly—go back to step 2 or 1.
4. Clear IntelliJ's build cache
Sometimes stale cache files mess up the build process:
- Go to File > Invalidate Caches...
- Select Invalidate and Restart
- Once IntelliJ reopens, rebuild your artifact from scratch
Give these steps a try—usually one of them fixes the issue when the main class is already present but the manifest isn't set up right.
内容的提问来源于stack exchange,提问作者James Jordan Taylor

