Windows 系统配置 GraalVM 的 native-image 环境

本文详细指导了如何在Windows环境下下载和配置GraalVM,包括设置JAVA_HOME和PATH,以及安装VisualStudio工具以支持native-image命令。重点解决了与多语言支持相关的问题和编译器版本要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于 GraalVM 和相关组件会更新变化,所以文章内容的有效性会受影响。

1. 下载 GraalVM

打开官网 https://www.graalvm.org/ 点击 Download 下载。

在这里插入图片描述

在当前时间(2023-11-29)我下载的版本为: graalvm-jdk-21_windows-x64_bin.zip

解压 GraalVM,例如目录 D:\Dev\graalvm-jdk-21.0.1+12.1

配置 JAVA_HOME 环境变量为 D:\Dev\graalvm-jdk-21.0.1+12.1,将 %JAVA_HOME%/bin 添加到 PATH 环境变量中,配置完成后执行下面命令验证:

>java -version
java version "21.0.1" 2023-10-17
Java(TM) SE Runtime Environment Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19, mixed mode, sharing)

>native-image --version
native-image 21.0.1 2023-10-17
GraalVM Runtime Environment Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19)
Substrate VM Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12, serial gc, compressed references)

2. Windows 环境安装 Visual Studio 2022 生成工具

想要编译 exe 还需要安装 Visual Studio and Microsoft Visual C++(MSVC),官方文档提供的下载地址(2019)版本比较老,运行时会出现 cl.exe 版本过低的问题,因此我们从下面地址下载最新版本(也会受时间影响)。

下载地址:https://visualstudio.microsoft.com/zh-hans/downloads/

打开下载地址后,在下面的所有下载中,展开 用于 Visual Studio 的工具

在这里插入图片描述

找到 Visual Studio 2022 生成工具,点击下载,下载后打开 vs_BuildTools.exe

在这里插入图片描述

该程序会安装 Visual Studio Installer,安装后打开如下界面:

在这里插入图片描述

勾选 使用 C++ 的桌面开发,在从右侧根据自己的系统版本选择最新的 Windows 11 SDKWindows 10 SDK,除此之外官方文档中还有个被选中的 Visual Studio SDK,不确定是否有影响,我们也额外选择这个,在 单个组件 中搜索勾选:

在这里插入图片描述

还有一个很关键的操作,点击 语言包,取消 中文(简体) 选项,勾选 英语,使用英语语言包。

在这里插入图片描述

参考:Error: Native-image building on Windows currently only supports target architecture: AMD64 (?? unsupported)
有可能是 GraalVM 对多语言的支持有问题,所以使用英语语言包。

点击安装,安装完成后重启系统。

3. 执行 native-image 命令测试

写一个简单例子:

public class TestNative {
    public static void main(String[] args) {
        System.out.println("hello native");
    }
}

编译 Java 代码为 class 时没有特别的要求,但是执行 native-image 需要特别注意。

在 Windows 中搜索 x64 Native Tools Command Prompt for VS 2022
在这里插入图片描述

以普通方式打开即可。

这一步也可以先使用 CMD 执行,如果遇到问题再换 x64 Native Tools Command Prompt for VS 2022 试试。

在这里插入图片描述

进入编译好的 class 目录,执行 native-image 命令:

>native-image TestNative
========================================================================================================================
GraalVM Native Image: Generating 'testnative' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing...                                                                                    (9.6s @ 0.13GB)
 Java version: 21.0.1+12, vendor version: Oracle GraalVM 21.0.1+12.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: cl.exe (microsoft, x64, 19.38.33130)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 1 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
------------------------------------------------------------------------------------------------------------------------
Build resources:
 - 21.38GB of memory (53.9% of 39.68GB system memory, determined at start)
 - 16 thread(s) (100.0% of 16 available processor(s), determined at start)
[2/8] Performing analysis...  [******]                                                                   (8.0s @ 0.20GB)
    2,087 reachable types   (61.3% of    3,402 total)
    1,994 reachable fields  (45.7% of    4,365 total)
    9,638 reachable methods (38.5% of   25,031 total)
      768 types,   109 fields, and   474 methods registered for reflection
       53 types,    30 fields, and    48 methods registered for JNI access
        1 native library: version
[3/8] Building universe...                                                                               (1.2s @ 0.28GB)
[4/8] Parsing methods...      [**]                                                                       (2.2s @ 0.32GB)
[5/8] Inlining methods...     [***]                                                                      (0.7s @ 0.32GB)
[6/8] Compiling methods...    [****]                                                                    (14.1s @ 0.34GB)
[7/8] Layouting methods...    [*]                                                                        (1.0s @ 0.36GB)
[8/8] Creating image...       [*]                                                                        (1.3s @ 0.38GB)
   3.50MB (47.56%) for code area:     4,547 compilation units
   3.78MB (51.33%) for image heap:   57,083 objects and 71 resources
  84.22kB ( 1.12%) for other data
   7.37MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   1.84MB java.base                                          891.79kB byte[] for code metadata
   1.35MB svm.jar (Native Image)                             722.13kB byte[] for java.lang.String
  90.68kB com.oracle.svm.svm_enterprise                      429.80kB heap alignment
  42.32kB jdk.proxy3                                         382.85kB java.lang.String
  40.36kB jdk.proxy1                                         331.77kB java.lang.Class
  30.28kB org.graalvm.nativeimage.base                       153.84kB java.util.HashMap$Node
  29.82kB org.graalvm.collections                            114.01kB char[]
  21.47kB jdk.internal.vm.ci                                 100.73kB byte[] for reflection metadata
  17.29kB jdk.internal.vm.compiler                            91.66kB java.lang.Object[]
  11.80kB jdk.proxy2                                          81.52kB com.oracle.svm.core.hub.DynamicHubCompanion
  389.00B for 1 more packages                                571.89kB for 552 more object types
                              Use '-H:+BuildReport' to create a report with more details.
------------------------------------------------------------------------------------------------------------------------
Security report:
 - Binary does not include Java deserialization.
 - Use '--enable-sbom' to embed a Software Bill of Materials (SBOM) in the binary.
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 INIT: Adopt '--strict-image-heap' to prepare for the next GraalVM release.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------------------
                        1.6s (3.9% of total time) in 216 GCs | Peak RSS: 0.93GB | CPU load: 6.20
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 D:\xxxxxxxxx\testnative.exe (executable)
========================================================================================================================
Finished generating 'testnative' in 39.2s.

控制台执行 testnative.exe 会输出 hello native

到这里就配置完成可以正常使用了。

4. 总结

我在安装过程中主要遇到下面3个问题:

  1. 安装VS CODE 2019工具时要使用默认路径,更改路径会导致找不到 bat 脚本,无法运行。
  2. 使用官方文档链接下载的 vs_buildtools__9e116ae0599f413e8e5d9edd8a3e7929.exe,这个工具安装的 VS CODE 2019 版本,安装的 cl.exe 版本低,会报下面的错误:
    Error: On Windows, GraalVM Native Image for JDK 21 requires Visual Studio 2022 version 17.1.0 or later (C/C++ Optimizing Compiler Version 19.31 or later).
    Compiler info detected: cl.exe (microsoft, ??, 19.29.30153)
    
    解决办法就是安装 VS CODE 2022版本,这个版本的 cl.exe 版本如下:
    在这里插入图片描述
  3. 第2个问题就是 Error: Native-image building on Windows currently only supports target architecture: AMD64 (?? unsupported),这个问题搜到了 github 上的 Issues,按照里面的操作选择英文语言就可以了。问题的原因可能是因为不支持中文,导致获取操作系统类型时出现了乱码。这一步使用普通的 CMD 也可以执行命令,关键的地方在于语言包。
<think>好的,我现在需要帮用户解决关于GraalVM native-image缺失的问题。首先,用户遇到了错误提示,指出GraalVM native-image不在系统中,并且GRAALVM_HOME环境变量未设置。我要确保用户能够正确安装和配置这些内容。 首先,我应该分步骤引导用户。第一步可能是确认GraalVM是否正确安装。用户可能下载了GraalVM但未正确设置环境变量,或者安装过程中遗漏了某些步骤。需要指导他们检查安装目录是否存在,以及是否解压到了正确的位置。 接下来,设置GRAALVM_HOME环境变量是关键。不同的操作系统有不同的设置方法,我需要分别说明Windows、macOS/Linux的情况。例如,在Windows上,用户需要通过系统属性设置环境变量,而在macOS/Linux则需修改bash或zsh的配置文件。这里要详细说明每一步操作,避免用户混淆。 然后是安装native-image工具。用户可能不知道这个工具需要单独安装,即使安装了GraalVM本身。需要告诉他们使用gu命令来安装,并且确保GraalVM的版本支持该命令。如果用户安装的是较旧的版本,可能需要先升级GraalVM或者安装对应的组件。 验证步骤也很重要,用户完成安装和配置后,需要检查环境变量是否生效,以及native-image是否可用。给出具体的命令示例,比如echo $GRAALVM_HOME和native-image --version,可以帮助用户确认是否正确设置。 另外,常见问题部分需要考虑用户可能遇到的典型错误,比如未找到gu命令,这可能是因为GraalVM没有正确安装或者环境变量未生效。或者构建失败,可能需要检查依赖项是否安装,如GCC等。这时候需要给出相应的解决建议,比如安装必要的依赖库。 最后,提醒用户如果所有步骤都正确但问题依旧存在,建议查阅官方文档或社区支持。这部分可以避免用户陷入更复杂的问题中,同时引导他们获取更多帮助资源。 整个过程中,要保持语言简洁明了,步骤清晰,避免使用专业术语过多,确保用户能够按照步骤一步步操作。同时,检查是否有遗漏的情况,比如不同操作系统的差异,或者版本不同导致的命令变化。确保覆盖所有可能的用户操作环境,提高解决问题的成功率。</think>关于GraalVM native-image缺失的问题,以下是逐步解决方案: 1. **确认GraalVM安装状态** - 访问[GraalVM官网](https://www.graalvm.org/)下载对应版本 - 检查安装目录是否存在(默认路径示例): - Linux/macOS: `/path/to/graalvm-ce-javaXX-XX.X.X` - Windows: `C:\Program Files\GraalVM\graalvm-ce-javaXX-XX.X.X` 2. **设置环境变量** - **Windows**: ```powershell # 设置永久环境变量 [System.Environment]::SetEnvironmentVariable('GRAALVM_HOME', 'C:\path\to\graalvm', 'Machine') # 添加PATH变量 $PATH = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') [System.Environment]::SetEnvironmentVariable('Path', "$PATH;$GRAALVM_HOME\bin", 'Machine') ``` - **macOS/Linux**: ```bash # 在 ~/.bashrc 或 ~/.zshrc 中添加 export GRAALVM_HOME=/path/to/graalvm export PATH=$GRAALVM_HOME/bin:$PATH # 立即生效 source ~/.bashrc ``` 3. **安装native-image工具** ```bash # 使用GraalVM自带的包管理器 gu install native-image ``` 4. **验证安装** ```bash # 检查环境变量 echo $GRAALVM_HOME # 验证native-image native-image --version ``` **常见问题排查**: - 若出现`gu: command not found`: - 确认`$GRAALVM_HOME/bin`已加入PATH - 重新启动终端/命令行窗口 - 构建失败时: ```bash # Linux需要安装额外依赖 sudo apt-get install build-essential libz-dev zlib1g-dev ``` **注意事项**: - 不同JDK版本需要对应GraalVM版本(如Java 11对应graalvm-java11) - 首次构建可能需要较长时间(涉及静态分析) - 建议使用最新LTS版本以获得最佳兼容性 若完成以上步骤仍存在问题,建议查阅官方文档: $$ \text{GraalVM Native Image官方指南} \rightarrow https://www.graalvm.org/latest/reference-manual/native-image/ $$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值