LHLY
发布于 2024-02-29 / 67 阅读
0

记录Windows 11 中使用 Springboot 3.2.x 及 GraalVM JDK 21 编译 Native Image 遇到的问题

正常流程

一、环境准备

  • Windows 11

  • GraalVM JDK 21

  • Maven 3.8.6

  • SpringBoot 3.2.3

  • Visual Studio 2022 v17.9.2

二、创建项目

  1. 创建 Springboot 项目,添加 GraalVM Native Support 依赖

  2. 写一个测试接口,方便测试能否成功调用

  3. 先试运行,确认能正常运行后准备使用 GraalVM native image 工具进行编译

三、编译

  1. 打开 x64 Native Tools Command Prompt for VS 2022 工具(以下简称“工具”)

  2. 在工具中定位到项目文件夹

  3. 输入命令开始编译:mvn clean native:compile -Pnative -DskipTests

  4. 此时可能会报错

    Execution of D:\path\Java\graalvm-jdk-21\bin\native-image.cmd @target\tmp\native-image-6834128070081939723.args returned non-zero result

    1. 出现这个错误需要打开 项目文件夹\target\tmp\native-image-6834128070081939723.args

    2. 在这个文件中将 \\ 全局替换为 /,并保存

    3. 回到工具中,输入报错的命令native-image @target\tmp\native-image-6834128070081939723.args,并回车执行

  5. 耐心等待编译成功,成功后运行 EXE 文件,并调用接口测试是否成功

遇到的坑

  1. 使用 GraalVM JDK 21 时,必须使用 Visual Studio 2022 v17.1.0 以上版本,否则在执行 native-image 命令时会有提示

  2. 在 IDEA 中使用 native-maven-plugin 插件执行 native:compile 指令,也会出现上面第4步出现的错误。

    此时修改了 args 文件后不可在 IDEA 的终端窗口中直接执行 native-image 命令,否则会有提示:

  3. 执行 native-image 时可能出现如下错误:Native-image building on Windows currently only supports target architecture: AMD64 (?? unsupported)

    需要在 Visual Studio Installer 中勾选英语语言包并去掉中文语言包