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

Spring Boot 3.2.0生成Native Image时Windows环境构建失败求助

构建Spring Boot Native Image时的Windows环境问题

环境信息

  • Spring Boot 3.2.0
  • Oracle GraalVM 21.0.1+12.1(native-image 21.0.1)
  • Windows系统,已安装Visual Studio 2022 v17.8.3并初始化x64环境

Maven配置

<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.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.java4raju</groupId>
    <artifactId>springvirtualnativegraalvm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringVirtualNativeGraalVM</name>
    <description>Demo project for GraalVm</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        </plugins>
    </build>
</project>

执行命令与错误信息

执行构建命令:

mvnw -Pnative native:compile

出现以下错误:

Error: Failed to automatically set up Windows build environment.
Please make sure that Visual Studio 2022 version 17.1.0 or later is installed on your system. You can download it at https://visualstudio.microsoft.com/d
ownloads/. If this error persists, please try and run GraalVM Native Image in an x64 Native Tools Command Prompt or file a ticket.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.788 s
[INFO] Finished at: 2023-12-15T22:57:27+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.28:compile (default-cli) on project springvirtualnativegraalvm: Execution o
f C:\Program Files\Java\graalvm-jdk-21.0.1+12.1\bin\native-image.cmd @target\tmp\native-image-15258589517628325859.args returned non-zero result -> [Help
 1]

已确认的环境配置

已启动Visual Studio 2022开发者命令提示符,x64环境初始化完成:

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.8.3
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

//Please Note I am trying to run in window machine   

解决方案尝试

  • 检查环境变量优先级:在Visual Studio命令提示符中执行echo %PATH%,确认GraalVM的bin目录(C:\Program Files\Java\graalvm-jdk-21.0.1+12.1\bin)位于其他JDK路径之前,避免环境冲突。
  • 手动指定Visual Studio环境脚本路径:修改Maven的native插件配置,添加参数指定vcvars64.bat的路径(替换为你的Visual Studio实际安装路径):
    <plugin>
        <groupId>org.graalvm.buildtools</groupId>
        <artifactId>native-maven-plugin</artifactId>
        <configuration>
            <buildArgs>
                <buildArg>-Dgraalvm.nativeimage.windows.vcvars=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat</buildArg>
            </buildArgs>
        </configuration>
    </plugin>
    
  • 使用专用Native Tools命令提示符:找到系统中的「x64 Native Tools Command Prompt for VS 2022」(而非普通开发者提示符),启动后再执行构建命令,该提示符会更精准配置C++编译环境。
  • 验证Visual Studio组件完整性:打开Visual Studio Installer,确认已安装「Desktop development with C++」工作负载,包含MSVC v143生成工具、Windows SDK等核心组件,缺失则补充安装。
  • 清理缓存后重新构建:先执行mvnw clean清理Maven缓存,再重新运行构建命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 02:24:58