>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个问题:
安装VS CODE 2019工具时要使用默认路径,更改路径会导致找不到 bat 脚本,无法运行。
使用官方文档链接下载的 vs_buildtools__9e116ae0599f413e8e5d9edd8a3e7929.exe,这个工具安装的 VS CODE 2019 版本,安装的 cl.exe 版本低,会报下面的错误:
1 2
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 版本如下:
第2个问题就是 **Error: Native-image building on Windows currently only supports target architecture: AMD64 (?? unsupported)**,这个问题搜到了 github 上的 Issues,按照里面的操作选择英文语言就可以了。问题的原因可能是因为不支持中文,导致获取操作系统类型时出现了乱码。这一步使用普通的 CMD 也可以执行命令,关键的地方在于语言包。