PS:禁止拷貝形式轉(zhuǎn)載,轉(zhuǎn)載請以URL形式
1.簡介
win10 環(huán)境下安裝graalvm,并編譯一個可執(zhí)行文件
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 配置環(huán)境變量
MSVC : C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110
Path:%MSVC%\bin\Hostx64\x64
3.2 安裝 graalvm
下載
官網(wǎng)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解壓配置環(huán)境變量(同JDK配置一致)
JAVA_HOME:C:\java\graalvm-ce-java11-20.3.0
Path:%JAVA_HOME%\bincmd查看
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執(zhí)行下載
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. 測試編譯執(zhí)行程序
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.isofor from Microsoft.- Mount the image by opening
F:\Setup\SDKSetup.exedirectly.
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-imagetool 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
驅(qū)動器 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 字節(jié)
2 個目錄 63,402,217,472 可用字節(jié)
- 進入MSVC構建工具,編譯可執(zhí)行程序
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
驅(qū)動器 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 字節(jié)
2 個目錄 63,399,608,320 可用字節(jié)
- 執(zhí)行程序
C:\test>example.exe
The reversed string is: emosewa si egamI evitaN
