由于 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 SDK 或 Windows 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.1Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferredC 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 reflection53 types, 30 fields, and 48 methods registered for JNI access1 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 units3.78MB (51.33%) for image heap: 57,083 objects and 71 resources84.22kB ( 1.12%) for other data7.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 metadata1.35MB svm.jar (Native Image) 722.13kB byte[] for java.lang.String90.68kB com.oracle.svm.svm_enterprise 429.80kB heap alignment42.32kB jdk.proxy3 382.85kB java.lang.String40.36kB jdk.proxy1 331.77kB java.lang.Class30.28kB org.graalvm.nativeimage.base 153.84kB java.util.HashMap$Node29.82kB org.graalvm.collections 114.01kB char[]21.47kB jdk.internal.vm.ci 100.73kB byte[] for reflection metadata17.29kB jdk.internal.vm.compiler 91.66kB java.lang.Object[]11.80kB jdk.proxy2 81.52kB com.oracle.svm.core.hub.DynamicHubCompanion389.00B for 1 more packages 571.89kB for 552 more object typesUse '-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. 总结
我在安装过程中主要遇到下面2个问题:
- 使用官方文档链接下载的 vs_buildtools__9e116ae0599f413e8e5d9edd8a3e7929.exe,这个工具安装的 VS CODE 2019 版本,安装的
cl.exe
版本低,会报下面的错误:
解决办法就是安装 VS CODE 2022版本,这个版本的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)
cl.exe
版本如下:
- 第2个问题就是 Error: Native-image building on Windows currently only supports target architecture: AMD64 (?? unsupported),这个问题搜到了 github 上的 Issues,按照里面的操作选择英文语言就可以了。问题的原因可能是因为不支持中文,导致获取操作系统类型时出现了乱码。这一步使用普通的 CMD 也可以执行命令,关键的地方在于语言包。