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

IntelliJ中Spring Boot无法找到或加载主类问题求助

问题:Spring Boot应用启动失败及Maven编译报错

1. 启动报错信息

Error: Could not find or load main class com.example.springboot.JgsbrmqApplication
Caused by: java.lang.ClassNotFoundException: com.example.springboot.JgsbrmqApplication

2. 主类代码

package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JgsbrmqApplication {

    public static void main(String[] args) {
        SpringApplication.run(JgsbrmqApplication.class, args);
    }

}

3. Maven编译报错信息

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project jgsbrmq: Fatal error compiling: error: release version 17 not supported -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

4. pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jgsbrmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jgsbrmq</name>
    <description>Demo project for Spring Boot and RabbitMQ</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!--<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>-->
        </plugins>
    </build>

</project>

解决步骤

  • 检查并安装Java 17
    Spring Boot 3.2.0强制要求Java 17及以上版本。运行java -version和javac -version确认本地环境是否已安装Java 17,若未安装则先下载安装,并配置JAVA_HOME环境变量指向Java 17的安装目录。
  • 同步IDE的JDK配置
    在IDE中:
    • 打开项目结构设置,将Project SDK和Module SDK均设置为Java 17;
    • 检查Maven运行配置,确保使用的JRE是Java 17;
    • 执行"Reload Maven Project"操作,让IDE同步配置。
  • 重新执行Maven构建
    先运行mvn clean清理旧的编译产物,再执行mvn install完成项目编译和打包。
  • 验证主类路径
    确认主类文件的物理路径为src/main/java/com/example/springboot/JgsbrmqApplication.java,包声明与路径一致,Spring Boot Maven插件会自动识别主类,无需额外配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:54:54