graalvm win10 64位环境搭建

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)

  1. 下载安装
    https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/

  2. 配置选择


    image.png
  3. 配置环境变量
    MSVC : C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110
    Path:%MSVC%\bin\Hostx64\x64

3.2 安装 graalvm

  1. 下载
    官网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

  2. 解压配置环境变量(同JDK配置一致)
    JAVA_HOME:C:\java\graalvm-ce-java11-20.3.0
    Path:%JAVA_HOME%\bin

  3. 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

  1. cmd执行下载
C:\Users\Administrator> gu install native-image
Downloading: Component catalog from www.graalvm.org
....省略。当前我已安装,是从github下载可能有点慢
  1. 查看安装的插件
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:

  1. Download the SDK file GRMSDKX_EN_DVD.iso for from Microsoft.
  2. 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 the native-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.
  1. 编译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 可用字节
  1. 进入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 可用字节
  1. 执行程序
C:\test>example.exe
The reversed string is: emosewa si egamI evitaN
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容