GraalVM构建Spring Boot原生镜像报错:主类org.example.Main未找到
Spring Boot项目构建GraalVM原生镜像失败问题解决
问题详情
尝试构建Spring Boot项目的原生镜像,执行命令:
native-image --no-server -cp C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar org.example.Main
命令在x64 NATIVE Tools Command Prompt for VS 2022中以管理员身份运行,抛出错误:
======================================================================================================================== GraalVM Native Image: Generating 'org.example.main' (executable)... ======================================================================================================================== [1/7] Initializing... (0,0s @ 0,13GB) Error: Main entry point class 'org.example.Main' neither found on the classpath nor on the modulepath. classpath: 'C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar' modulepath: 'C:\Program Files (x86)\graalvm-ce-java17-22.3.1\lib\svm\library-support.jar' Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception Error: Image build request failed with exit status 1
主类org.example.Main存在于项目目录中,普通非Spring应用可正常通过native-image命令构建镜像。
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.0.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org</groupId> <artifactId>example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>example</name> <description>example</description> <properties> <java.version>17</java.version> <start-class>org.example.Main</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>org.example.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
Java版本信息
openjdk version "17.0.6" 2023-01-17 OpenJDK Runtime Environment GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13) OpenJDK 64-Bit Server VM GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13, mixed mode, sharing)
解决方案
1. 使用Maven插件自动构建(推荐)
Spring Boot 3.x与GraalVM原生镜像的官方集成方案是通过native-maven-plugin完成,无需手动编写native-image命令。在项目根目录执行:
mvn clean native:compile -Pnative
该命令会自动处理类路径解析、主类检测、Spring Boot原生元数据生成等步骤,彻底避免手动命令的路径和类加载问题。
2. 手动执行native-image命令的修正方式
Spring Boot生成的标准Jar是嵌套结构的可执行Jar,直接用-cp指定无法正确定位主类,可通过两种方式修正:
方式一:指定Spring Boot的启动加载类
native-image --no-server -cp C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar org.springframework.boot.loader.launch.JarLauncher
方式二:解压Jar后构建
# 解压Jar到临时目录 unzip C:\Users\Andrey\IdeaProjects\example\target\example-0.0.1-SNAPSHOT.jar -d temp-native-dir # 进入解压目录执行构建 cd temp-native-dir native-image --no-server -cp . org.example.Main
3. 优化pom配置确保主类识别
可在native-maven-plugin中明确指定主类,避免自动检测异常:
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <mainClass>org.example.Main</mainClass> </configuration> </plugin>
内容的提问来源于stack exchange,提问作者Andrey Picasso
相关产品推荐
相关产品推荐

