Spring Boot 项目执行 maven 打包 package 时,抛出“Unable to find a single main class from the following candidates [com.hxstrive.ehcache.HelloEhcache, com.hxstrive.ehcache.HelloEhcache2]”异常

本文将介绍如何解决“Unable to find a single main class from the following candidates [com.hxstrive.ehcache.HelloEhcache, com.hxstrive.ehcache.HelloEhcache2]”异常。

项目打包编译时出现如下错误:

Unknown Title

错误信息:

Unable to find a single main class from the following candidates [com.hxstrive.ehcache.HelloEhcache, com.hxstrive.ehcache.HelloEhcache2]

原因分析:

项目通过 <parent> 继承 Spring Boot 的 pom.xml 来开发,如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--<version>2.0.1.RELEASE</version>-->
    <version>2.6.15</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

但是在项目主目录,即 Spring Boot 项目的启动类目录存在多个拥有 main() 方法的类,而且我们没有手动为项目指定启动类,这就导致项目编译打包时无法确定到底使用哪个拥有 main() 方法的类。

解决办法:

在 pom.xml 文件的 <properties> 标签中,通过 <start-class> 手动指定启动类,就可以解决:

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.hxstrive.ehcache.HelloEhcache</start-class>
    <!-- ... -->
</properties>

注意:这种方式只适合直接继承 Spring Boot 的项目。

一知半解的人,多不谦虚;见多识广有本领的人,一定谦虚。——谢觉哉
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号