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

无法通过命令行运行Spring Boot项目:需添加mainClass属性

Maven spring-boot:run 找不到主类,同时关于IntelliJ运行机制的疑问

日常用IntelliJ IDEA开发Spring Boot项目,今天在pom.xml所在目录执行mvn spring-boot:run命令启动项目失败,报错提示需要添加'mainClass'属性,但项目在IDE中能正常运行。之前这个命令是可以成功运行的,调整过Spring Boot版本后改回原配置就失效了,试过网上的相关解决方案但问题仍未解决。

另外还有两个疑问:

  1. IntelliJ不需要配置spring-boot插件就能运行项目,它是怎么做到的?
  2. 其他没有配置spring-boot插件的项目在IDE中能正常运行,但无法通过mvn spring-boot:run命令启动,想知道IntelliJ运行这些项目时用的是什么命令。

项目信息

项目结构

  • HelloWorld/src/main/java/Example.java
  • HelloWorld/pom.xml

Java版本

java version "11.0.12" 2021-07-20 LTS

Example.java代码

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example{
@RequestMapping("/sayHello")
String sayHello(){
return "Hello World";
}

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

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>

<parent>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

错误日志

[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot:1.5.7.RELEASE:run (default-cli) > test-compile @ myproject >>>
[INFO]
[INFO] --- resources:2.6:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\main\resources
[INFO]
[INFO] --- compiler:3.1:compile (default-compile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- resources:2.6:testResources (default-testResources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\test\resources
[INFO]
[INFO] --- compiler:3.1:testCompile (default-testCompile) @ myproject ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot:1.5.7.RELEASE:run (default-cli) < test-compile @ myproject <<<
[INFO]
[INFO]
[INFO] --- spring-boot:1.5.7.RELEASE:run (default-cli) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.093 s
[INFO] Finished at: 2024-04-13T20:03:57+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project myproject: Unable to find a suitable main class, please add a 'mainClass' property -> [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

内容的提问来源于stack exchange,提问作者Temel Tahir Isiltan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 00:45:13