Spring Boot迁移GraalVM构建触发NullPointerException求助
Spring Boot迁移GraalVM Native构建空指针异常求助
将Spring Boot项目迁移至GraalVM并执行Native构建时,触发如下空指针异常,暂未找到可行解决办法,恳请社区协助:
java.lang.NullPointerException: Cannot invoke "java.util.Set.iterator()" because "triggers" is null at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.ReachabilityHandlerFeature.processReachable(ReachabilityHandlerFeature.java:173) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.ReachabilityHandlerFeature.duringAnalysis(ReachabilityHandlerFeature.java:128) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$10(NativeImageGenerator.java:770) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:86) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$11(NativeImageGenerator.java:770) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.AbstractAnalysisEngine.runAnalysis(AbstractAnalysisEngine.java:179) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:767) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:582) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:539) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:408) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:612) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.start(NativeImageGeneratorRunner.java:134) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:94)
环境信息
- Spring Boot版本:3.1.1
- JDK:GraalVM JDK 17
- 平台:MacOS(M2,Aarch64)
Maven插件配置
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <extensions>true</extensions> <executions> <execution> <id>build-native</id> <goals> <goal>compile-no-fork</goal> </goals> <phase>package</phase> </execution> <execution> <id>test-native</id> <goals> <goal>test</goal> </goals> <phase>test</phase> </execution> </executions> <configuration> <fallback>false</fallback> <buildArgs> <arg> --verbose -Dspring.aot.enabled=true -H:TraceClassInitialization=true -H:+ReportExceptionStackTraces -H:Name=trace-app -H:DashboardDump=trace-app -H:+DashboardAll -H:Class=com.xxx.trace.TraceApplication --initialize-at-build-time=org.slf4j.LoggerFactory,ch.qos.logback,org.slf4j.MDC,org.slf4j.impl.StaticLoggerBinder --initialize-at-run-time=io.netty -Dspring.graal.remove-unused-autoconfig=true -Dspring.graal.remove-yaml-support=true </arg> </buildArgs> <agent> <enabled>true</enabled> <defaultMode>Standard</defaultMode> <options> <trackReflectionMetadata>true</trackReflectionMetadata> </options> <metadataCopy> <disabledStages> <stage>test</stage> </disabledStages> <merge>true</merge> <outputDirectory>/tmp/test-output-dir</outputDirectory> </metadataCopy> </agent> </configuration> </plugin>
解决建议
1. 升级GraalVM版本
该异常是GraalVM Native Image构建阶段ReachabilityHandlerFeature内部的空指针,属于GraalVM自身bug。建议升级到GraalVM 17的最新稳定版本(如22.3.4或23.0.1),旧版本存在此类分析阶段的空指针问题。
2. 简化构建参数
当前配置的自定义参数过多,逐步移除非必要参数排查:
- 移除
--initialize-at-build-time和--initialize-at-run-time的自定义配置,使用Spring Boot默认的初始化策略 - 去掉
-H:+DashboardAll、-H:DashboardDump等调试参数,减少构建过程中的额外分析负载 - 删除
-Dspring.aot.enabled=true,Spring Boot 3.x配合native插件会自动启用AOT,无需手动指定
3. 检查Agent元数据有效性
当前配置了Agent生成元数据,需确保:
/tmp/test-output-dir目录有读写权限- 元数据文件(
reflect-config.json、resource-config.json等)生成完整且无损坏 - 可临时关闭Agent(设置
<enabled>false</enabled>),先用Spring Boot自动生成的AOT元数据完成构建,再逐步添加Agent元数据
4. 排查依赖兼容性
检查项目中是否存在与GraalVM Native Image不兼容的依赖,尤其是自定义Starter或第三方库。可临时移除非核心依赖,逐步定位是否为某个依赖触发异常。
5. 切换为插件默认配置
先简化插件配置到基础形式,确保构建正常运行后再逐步添加自定义参数:
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <extensions>true</extensions> </plugin>
内容的提问来源于stack exchange,提问作者Anurag Ambuj
相关产品推荐
相关产品推荐

