PS:禁止拷贝形式转载,转载请以URL形式
1.简介
win10 环境下安装graalvm,并编译一个可执行文件
2.参考
https://www.graalvm.org/docs/getting-started/
https://www.graalvm.org/reference-manual/native-image/
3.安装
3.1安装Microsoft Visual C ++(MSVC)
-
下载安装
https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/ -
配置选择
image.png -
配置环境变量
MSVC : C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110
Path:%MSVC%\bin\Hostx64\x64
3.2 安装 graalvm
-
下载
官网URL:https://www.graalvm.org/downloads/
有两个版本,分别以java8/java11 进行构建,本教程选择java11版本进行构建
java11:https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-20.3.0/graalvm-ce-java11-windows-amd64-20.3.0.zip -
解压配置环境变量(同JDK配置一致)
JAVA_HOME:C:\java\graalvm-ce-java11-20.3.0
Path:%JAVA_HOME%\bin -
cmd查看
C:\Users\Administrator>java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)
3.2 安装 native-image
- cmd执行下载
C:\Users\Administrator> gu install native-image
Downloading: Component catalog from www.graalvm.org
....省略。当前我已安装,是从github下载可能有点慢
- 查看安装的插件
C:\Users\Administrator>gu list
ComponentId Version Component name Origin
--------------------------------------------------------------------------------
js 20.3.0 Graal.js
graalvm 20.3.0 GraalVM Core
native-image 20.3.0 Native Image github.com
4. 测试编译执行程序
4.1 测试源代码
public class Example {
public static void main(String[] args) {
String str = "Native Image is awesome";
String reversed = reverseString(str);
System.out.println("The reversed string is: " + reversed);
}
public static String reverseString(String str) {
if (str.isEmpty())
return str;
return reverseString(str.substring(1)) + str.charAt(0);
}
}
4.2 编译
PS:核心参考
https://www.graalvm.org/docs/getting-started/windows/
Prerequisites for Using Native Image on Windows
To make use of Native Image on Windows, observe the following recommendations. The required Microsoft Visual C++ (MSVC) version depends on the JDK version that GraalVM is based on. For GraalVM based on JDK 8, you will need MSVC 2010 SP1 version. The recommended installation method is using Microsoft Windows SDK 7.1:
- Download the SDK file
GRMSDKX_EN_DVD.iso
for from Microsoft.- Mount the image by opening
F:\Setup\SDKSetup.exe
directly.
For GraalVM distribution based on JDK 11, you will need MSVC 2017 15.5.5 or later version.
The last prerequisite, common for both distributions, is the proper Developer Command Prompt for your version of Visual Studio. On Windows thenative-image
tool only works when it is executed from the x64 Native Tools Command Prompt.
https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160&viewFallbackFrom=vs-2019#developer_command_prompt_shortcuts
Developer command file locations
If you prefer to set the build environment in an existing command prompt window, you can use one of the command files created by the installer. We recommend you set the environment in a new command prompt window. We don't recommend you later switch environments in the same command window.
The command file location depends on the version of Visual Studio you installed, and on choices you made during installation. For Visual Studio 2019, the typical installation location on a 64-bit system is in \Program Files (x86)\Microsoft Visual Studio\2019\edition. Edition may be Community, Professional, Enterprise, BuildTools, or another nickname you supplied.
The primary developer command prompt command file, VsDevCmd.bat, is located in the Common7\Tools subdirectory. When no parameters are specified, it sets the environment to use the x86-native tools to build 32-bit x86 code.
More command files are available to set up specific build architectures. The command files available depend on the Visual Studio workloads and options you've installed. In Visual Studio 2017 and Visual Studio 2019, you'll find them in the VC\Auxiliary\Build subdirectory.
These command files set default parameters and call VsDevCmd.bat to set up the specified build architecture environment. A typical installation may include these command files:
Command File Host and Target architectures vcvars32.bat Use the 32-bit x86-native tools to build 32-bit x86 code. vcvars64.bat Use the 64-bit x64-native tools to build 64-bit x64 code. vcvarsx86_amd64.bat Use the 32-bit x86-native cross tools to build 64-bit x64 code. vcvarsamd64_x86.bat Use the 64-bit x64-native cross tools to build 32-bit x86 code. vcvarsx86_arm.bat Use the 32-bit x86-native cross tools to build ARM code. vcvarsamd64_arm.bat Use the 64-bit x64-native cross tools to build ARM code. vcvarsall.bat Use parameters to specify the host and target architectures, Windows SDK, and platform choices. For a list of supported options, call by using a /help parameter.
- 编译class
C:\test>javac Example.java
C:\test>dir
驱动器 C 中的卷是 OS
卷的序列号是 761C-B98F
C:\test 的目录
2020/12/25 13:18 <DIR> .
2020/12/25 13:18 <DIR> ..
2020/12/25 13:18 1,233 Example.class
2020/12/24 15:52 525 Example.java
2 个文件 1,758 字节
2 个目录 63,402,217,472 可用字节
- 进入MSVC构建工具,编译可执行程序
C:\test>"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" amd64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.7.2
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\test>native-image Example
[example:8936] classlist: 1,223.08 ms, 0.96 GB
[example:8936] (cap): 1,513.15 ms, 0.96 GB
[example:8936] setup: 2,972.21 ms, 0.96 GB
[example:8936] (clinit): 164.97 ms, 1.20 GB
[example:8936] (typeflow): 4,434.05 ms, 1.20 GB
[example:8936] (objects): 3,540.23 ms, 1.20 GB
[example:8936] (features): 234.14 ms, 1.20 GB
[example:8936] analysis: 8,596.38 ms, 1.20 GB
[example:8936] universe: 359.30 ms, 1.22 GB
[example:8936] (parse): 1,037.49 ms, 1.22 GB
[example:8936] (inline): 1,126.91 ms, 1.66 GB
[example:8936] (compile): 5,862.36 ms, 2.25 GB
[example:8936] compile: 8,497.67 ms, 2.25 GB
[example:8936] image: 921.05 ms, 2.25 GB
[example:8936] write: 289.03 ms, 2.25 GB
[example:8936] [total]: 22,996.00 ms, 2.25 GB
C:\test>dir
驱动器 C 中的卷是 OS
卷的序列号是 761C-B98F
C:\test 的目录
2020/12/25 13:19 <DIR> .
2020/12/25 13:19 <DIR> ..
2020/12/25 13:18 1,233 Example.class
2020/12/25 13:19 8,368,128 example.exe
2020/12/25 13:19 76,169 example.exp
2020/12/24 15:52 525 Example.java
2020/12/25 13:19 125,478 example.lib
2020/12/25 13:19 798,720 example.pdb
2020/12/25 13:19 290,816 example.stripped.pdb
7 个文件 9,661,069 字节
2 个目录 63,399,608,320 可用字节
- 执行程序
C:\test>example.exe
The reversed string is: emosewa si egamI evitaN
网友评论