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

GraalVM JDK22构建Spring Boot原生镜像遇UnsupportedFeatureException

问题

使用graalvm-jdk-22.0.1+8.1构建Spring Boot原生容器镜像时,遇到以下错误:

[INFO]     [creator]     ================================================================================
[INFO]     [creator]     GraalVM Native Image: Generating 'com.example.demo.DemoApplication' (executable)...
[INFO]     [creator]     ================================================================================
[INFO]     [creator]     For detailed information and explanations on the build output, visit:
[INFO]     [creator]     https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
[INFO]     [creator]     --------------------------------------------------------------------------------
[INFO]     [creator]     [1/8] Initializing...                                            (9.2s @ 0.13GB)
[INFO]     [creator]      Java version: 22.0.1+10, vendor version: Liberica-NIK-24.0.1-1
[INFO]     [creator]      Graal compiler: optimization level: 2, target machine: x86-64-v3
[INFO]     [creator]      C compiler: gcc (linux, x86_64, 11.4.0)
[INFO]     [creator]      Garbage collector: Serial GC (max heap size: 80% of RAM)
[INFO]     [creator]      2 user-specific feature(s):
[INFO]     [creator]      - com.oracle.svm.thirdparty.gson.GsonFeature
[INFO]     [creator]      - org.springframework.aot.nativex.feature.PreComputeFieldFeature
[INFO]     [creator]     --------------------------------------------------------------------------------
[INFO]     [creator]      2 experimental option(s) unlocked:
[INFO]     [creator]      - '-H:Name' (alternative API option(s): -o com.example.demo.DemoApplication; origin(s): command line)
[INFO]     [creator]      - '-H:+StaticExecutableWithDynamicLibC' (origin(s): command line)
[INFO]     [creator]     --------------------------------------------------------------------------------
[INFO]     [creator]     Build resources:
[INFO]     [creator]      - 8.83GB of memory (56.9% of 15.53GB system memory, determined at start)
[INFO]     [creator]      - 8 thread(s) (100.0% of 8 available processor(s), determined at start)
[INFO]     [creator]     SLF4J(W): No SLF4J providers were found.
[INFO]     [creator]     SLF4J(W): Defaulting to no-operation (NOP) logger implementation
[INFO]     [creator]     SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
[INFO]     [creator]     [2/8] Performing analysis...  []                                (51.5s @ 0.64GB)
[INFO]     [creator]         7,254 reachable types   (79.7% of    9,106 total)
[INFO]     [creator]         8,510 reachable fields  (48.4% of   17,574 total)
[INFO]     [creator]        30,520 reachable methods (51.2% of   59,658 total)
[INFO]     [creator]         2,514 types,   131 fields, and 1,782 methods registered for reflection
[INFO]     [creator]     
[INFO]     [creator]     Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type 'org.springframework.boot.loader.nio.file.NestedFileSystemProvider' was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default.
[INFO]     [creator]     This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time.
[INFO]     [creator]     
[INFO]     [creator]     You now have two options to resolve this:
[INFO]     [creator]     
[INFO]     [creator]     1) If it is intended that objects of type 'org.springframework.boot.loader.nio.file.NestedFileSystemProvider' are persisted in the image heap, add 
[INFO]     [creator]     
[INFO]     [creator]         '--initialize-at-build-time=org.springframework.boot.loader.nio.file.NestedFileSystemProvider'
[INFO]     [creator]     
[INFO]     [creator]     to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of 'org.springframework.boot.loader.nio.file.NestedFileSystemProvider' to see if they are safe for build-time initialization,  and that they do not contain any sensitive data that should not become part of the image.
[INFO]     [creator]     
[INFO]     [creator]     2) If these objects should not be stored in the image heap, you can use 
[INFO]     [creator]     
[INFO]     [creator]         '--trace-object-instantiation=org.springframework.boot.loader.nio.file.NestedFileSystemProvider'
[INFO]     [creator]     
[INFO]     [creator]     to find classes that instantiate these objects. Once you found such a class, you can mark it explicitly for run time initialization with 
[INFO]     [creator]     
[INFO]     [creator]         '--initialize-at-run-time=<culprit>'
[INFO]     [creator]     
[INFO]     [creator]     to prevent the instantiation of the object.
[INFO]     [creator]     
[INFO]     [creator]     If you are seeing this message after upgrading to a new GraalVM release, this means that some objects ended up in the image heap without their type being marked with --initialize-at-build-time.
[INFO]     [creator]     To fix this, include '--initialize-at-build-time=org.springframework.boot.loader.nio.file.NestedFileSystemProvider' in your configuration. If the classes do not originate from your code, it is advised to update all library or framework dependencies to the latest version before addressing this error.
[INFO]     [creator]     
[INFO]     [creator]     The following detailed trace displays from which field in the code the object was reached.
[INFO]     [creator]     Object was reached by
[INFO]     [creator]       manually created constant
[INFO]     [creator]     --------------------------------------------------------------------------------
[INFO]     [creator]         6.1s (9.6% of total time) in 130 GCs | Peak RSS: 1.21GB | CPU load: 6.54
[INFO]     [creator]     ================================================================================
[INFO]     [creator]     Failed generating 'com.example.demo.DemoApplication' after 1m 1s.
[INFO]     [creator]     unable to invoke layer creator
[INFO]     [creator]     unable to contribute native-image layer
[INFO]     [creator]     error running build
[INFO]     [creator]     exit status 1
[INFO]     [creator]     ERROR: failed to build: exit status 1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:41 min
[INFO] Finished at: 2024-05-14T13:34:10+02:00
[INFO] ------------------------------------------------------------------------

已尝试官方建议的--initialize-at-build-time参数,但问题未解决。

复现步骤:

  • 访问Spring Initializr
  • 选择:Maven、Java、Spring Boot 3.2.5、Jar打包方式、Java 22
  • 添加“GraalVM Native Support”依赖
  • 下载并解压项目
  • 运行命令./mvnw -Pnative -DskipTests spring-boot:build-image
  • 出现上述错误

需解决该问题,以使用Java 22、GraalVM JDK成功构建Spring Boot项目的原生容器镜像。

解决方案

方案1:升级Spring Boot版本

Spring Boot 3.2.5存在与Java 22、GraalVM 22兼容的问题,升级到**Spring Boot 3.2.6+**或更高版本(如3.3.x)可修复该类初始化冲突问题。修改pom.xml中的Spring Boot版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.6</version> <!-- 或3.3.0+ -->
    <relativePath/> <!-- lookup parent from repository -->
</parent>

修改后重新执行构建命令即可。

方案2:扩展构建时初始化配置(无法升级版本时)

若不能升级Spring Boot,可在pom.xml的native-maven-plugin中添加更全面的构建时初始化配置,覆盖相关包的初始化时机:

<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <buildArgs>
            <buildArg>--initialize-at-build-time=org.springframework.boot.loader.nio.file.NestedFileSystemProvider</buildArg>
            <buildArg>--initialize-at-build-time=org.springframework.boot.loader</buildArg>
        </buildArgs>
    </configuration>
</plugin>

也可直接通过Maven命令传递参数:

./mvnw -Pnative -DskipTests -Dnative.buildArgs="--initialize-at-build-time=org.springframework.boot.loader.nio.file.NestedFileSystemProvider,org.springframework.boot.loader" spring-boot:build-image

方案3:检查并使用默认Native插件配置

确保spring-boot-maven-plugin的版本与Spring Boot版本完全一致,避免手动添加可能引发冲突的自定义参数,依赖Spring Boot自动生成的Native镜像配置。

内容的提问来源于stack exchange,提问作者rbarriuso

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:52:02