You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ IDEA打包Spring Boot Jar异常:无法找到或加载主类

Fixing "Could not find or load main class" for Spring Boot Executable JAR

Hey there, I’ve run into this exact issue a handful of times when working with Spring Boot, so let’s break down the most likely fixes to get your JAR running properly:

1. Verify Your Build Tool Configuration (Maven/Gradle)

The biggest culprit here is usually missing or misconfigured Spring Boot packaging plugin. Spring Boot requires its own plugin to create an executable "fat jar" that includes all dependencies and sets up the correct main class entry.

For Maven:

Make sure your pom.xml includes the Spring Boot Maven plugin with proper main class configuration (replace com.yourpackage.Main with your actual full class path):

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.yourpackage.Main</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

For Gradle:

Check your build.gradle has the bootJar configuration set:

plugins {
    id 'org.springframework.boot' version 'your-spring-boot-version'
    id 'io.spring.dependency-management' version 'your-dependency-management-version'
    id 'java'
}

bootJar {
    mainClass = 'com.yourpackage.Main'
}

2. Confirm You’re Building an Executable Fat Jar

Not all JARs are created equal—Spring Boot’s executable JAR has a specific structure. Run this command to inspect your JAR’s contents:

jar tf your-app.jar

Look for two key things:

  • BOOT-INF/classes/com/yourpackage/Main.class: This confirms your compiled main class is present in the correct location.
  • META-INF/MANIFEST.MF: Open this file (you can use jar xf your-app.jar META-INF/MANIFEST.MF to extract it) and check for these lines:
    Main-Class: org.springframework.boot.loader.JarLauncher
    Start-Class: com.yourpackage.Main
    

If these lines are missing, your JAR isn’t a proper Spring Boot executable JAR—recheck your build plugin setup.

3. Use the Correct Run Command

Never try to run the Main.class file directly—Spring Boot relies on its JarLauncher to handle classpath and dependency loading. The only correct command to run your JAR is:

java -jar your-app.jar

Double-check you’re running this from the directory where your JAR is located, or use the full file path (e.g., java -jar target/your-app-0.0.1-SNAPSHOT.jar).

4. Double-Check Your Main Class Package Path

It’s easy to overlook the full package name when specifying the main class. For example, if your Main.java is in the com.myapp.demo package, the full main class name must be com.myapp.demo.Main—not just Main.

5. Clean and Rebuild to Clear Caches

Sometimes old compiled files or cached build artifacts cause issues. In IntelliJ:

  • Go to Build > Clean Project to wipe old build files.
  • Then run your build tool’s package command (for Maven: mvn package; for Gradle: gradle bootJar).
  • You can also manually delete the target (Maven) or build (Gradle) directory before rebuilding to ensure a fresh start.

Give these steps a try—9 times out of 10, the issue is either a misconfigured build plugin or a missing package name in the main class setup.

内容的提问来源于stack exchange,提问作者user12860315

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:22:02