一、SpringBoot 打包 jar 运行报错

SpringBoot 项目打包成 jar 包后,运行时可能会出现报错:“no main manifest attribute,in XXX.jar”,即没有主清单属性,这是由于在打包时没有指定 main class,导致 jar 包里没有主清单属性。

二、主清单属性的作用

主清单属性是指在 jar 包的 META-INF/MANIFEST.MF 文件中的 Main-Class 属性,它指定了 jar 包的入口类,可以让 java 命令可以执行 jar 包,而不需要指定主类。

三、解决方案

1、在 pom.xml 文件中添加 maven-jar-plugin 插件,指定主类:

<plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-jar-plugin</artifactId>  <configuration>    <archive>      <manifest>        <mainClass>com.xxx.xxx.xxx.xxx</mainClass>      </manifest>    </archive>  </configuration></plugin>
XML

2、在 application.properties 文件中添加配置:

start-class=com.xxx.xxx.xxx.xxx
.properties

3、在 SpringBoot 的启动类上添加 @SpringBootApplication 注解,并在注解上添加 scanBasePackages 属性:

@SpringBootApplication(scanBasePackages = "com.xxx.xxx.xxx.xxx")public class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
Java

4、也可以在打包时使用命令指定主类:

mvn clean package -DmainClass="com.xxx.xxx.xxx.xxx"
Bash

以上 4 种方法都可以解决 SpringBoot 打包 jar 运行时提示没有主清单属性的问题。