native-image入门


目录
  • native-image入门
    • 一 摘要
    • 二 GraalVM和native-image介绍
    • 三 GraalVM安装
    • 四 native-image安装
    • 五 HelloWorld
      • 1. 代码
      • 2. 命令
    • 六 异常情况
      • 找不到cl.exe
      • 找不到***.h
      • 找不到***.lib

native-image入门

一 摘要

本文介绍了Graalvm, native-image在windows平台上的安装配置, 并编译helloworld.

二 GraalVM和native-image介绍

GraalVM介绍: 官网机翻:

GraalVM 是一个高性能 JDK 发行版,旨在加速用 Java 和其他 JVM 语言编写的应用程序的执行,同时支持 JavaScript、Ruby、Python 和许多其他流行语言。GraalVM 的多语言功能可以在单个应用程序中混合多种编程语言,同时消除外语调用成本。

GraalVM is a high-performance JDK distribution designed to accelerate the execution of applications written in Java and other JVM languages along with support for JavaScript, Ruby, Python, and a number of other popular languages. GraalVM’s polyglot capabilities make it possible to mix multiple programming languages in a single application while eliminating foreign language call costs.

GraalVM知乎的介绍

native-image官方文档

Native Image 是一种将 Java 代码提前编译为独立可执行文件(称为原生映像)的技术。此可执行文件包括应用程序类、其依赖项中的类、运行时库类以及来自 JDK 的静态链接本机代码。它不在 Java VM 上运行,但包含来自不同运行时系统(称为“Substrate VM”)的必要组件,如内存管理、线程调度等。Substrate VM 是运行时组件(如去优化器、垃圾收集器、线程调度等)的名称。与 JVM 相比,生成的程序具有更快的启动时间和更低的运行时内存开销。

百度随便找的文章

三 GraalVM安装

直奔主题.

官网下载页面

Github Community Edition 仓库

选择下载graalvm-ce-{java版本}-windows-amd64-{graalvm版本}.zip即可.

java11-22.0.0.2

java17-22.0.0.2

下载后解压到任意文件夹, 配置JAVA_HOME, 以及PATH环境变量即可.(可选, 不配置的话后续命令要输入完整路径.)

四 native-image安装

安装好GraalVM之后, 通过以下命令安装native-image

gu install native-image

你也可以通过下载github仓库中的native-image jar文件来手动安装, 这里不多赘述.

参考文档

安装完成后, 检查安装.

PS C:\Users\Administrator> .\native-image
Please specify options for native-image building or use --help for more info.
PS C:\Users\Administrator>

在windows平台运行native-image需要安装visual studio和MSVC.

要开始在 Windows 上使用 Native Image,请安装Visual Studio和 Microsoft Visual C++ (MSVC)。有两个安装选项: * 使用 Windows 10 SDK 安装 Visual Studio 构建工具 * 使用 Windows 10 SDK 安装 Visual Studio

您可以使用 Visual Studio 2017 版本 15.9 或更高版本。

最后,在 Windows 上,构建器仅在从x64 Native Tools Command Promptnative-image执行时才能工作。如果您只安装了 Visual Studio 构建工具,与安装了完整的 VS 2019 相比,启动 x64 Native Tools 命令提示符的命令是不同的。检查此链接以获取分步说明。

点击上面的引用中的连接下载安装visual studio以及阅读官方的详细文档, 他们的内容远比我丰富, 在下面我挑其中的重点说明.

  1. 下载visual studio installer 2017或以上版本.

  2. 通过visual studio installer安装windows10 SDK.

    image-20220331222744511

    image-20220331222835539

  3. 运行native-image之前先运行msvc环境变量配置脚本.

    image-20220331223020983

    通过win+s输入x64搜索该脚本, 该快捷方式指向的位置通常是C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat(如果你没有修改vs的安装位置的话, 你也可以通过everything搜索该文件.)

    值得注意的是, 该脚本只会临时为你设置环境变量, 如果你关闭了命令行窗口, 在新的窗口中你需要再次运行该脚本你才能正常使用native-iamge.

五 HelloWorld

1. 代码

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

2. 命令

运行于cmd.

javac HelloWorld.java
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
native-image HelloWorld
helloworld.exe

完整输入输出如下:

C:\Users\Administrator\Desktop\helloworld>javac HelloWorld.java

C:\Users\Administrator\Desktop\helloworld>"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.1.1
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Users\Administrator\Desktop\helloworld>native-image HelloWorld
[helloworld:7688]    classlist:     570.74 ms,  0.96 GB
[helloworld:7688]        (cap):   1,096.94 ms,  0.96 GB
[helloworld:7688]        setup:   2,226.80 ms,  0.96 GB
[helloworld:7688]     (clinit):      81.77 ms,  1.22 GB
[helloworld:7688]   (typeflow):   3,896.83 ms,  1.22 GB
[helloworld:7688]    (objects):   3,628.47 ms,  1.22 GB
[helloworld:7688]   (features):     207.98 ms,  1.22 GB
[helloworld:7688]     analysis:   7,916.73 ms,  1.22 GB
[helloworld:7688]     universe:     218.55 ms,  1.22 GB
[helloworld:7688]      (parse):     454.62 ms,  1.68 GB
[helloworld:7688]     (inline):     673.05 ms,  1.68 GB
[helloworld:7688]    (compile):   2,606.29 ms,  2.30 GB
[helloworld:7688]      compile:   3,993.59 ms,  2.30 GB
[helloworld:7688]        image:     652.44 ms,  2.30 GB
[helloworld:7688]        write:     111.28 ms,  2.30 GB
# Printing build artifacts to: helloworld.build_artifacts.txt
[helloworld:7688]      [total]:  15,796.31 ms,  2.30 GB

C:\Users\Administrator\Desktop\helloworld>helloworld.exe
hello world

C:\Users\Administrator\Desktop\helloworld>

所有文件:

image-20220331224534915

六 异常情况

无论出现哪种找不到文件的异常, 请先运行一遍vcvars64.bat脚本.

找不到cl.exe

Error: Default native-compiler executable 'cl.exe' not found via environment variable PATH
Error: To prevent native-toolchain checking provide command-line option -H:-CheckToolchain
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
Error: Image build request failed with exit status 1

解决方案:

  1. 运行vcvars64.bat
  2. 或者通过everything搜索cl.exe, 将其所在的目录加入PATH, 通常位于{visual studio安装路径}\VC\Tools\MSVC\{msvc版本}\bin\Hostx64\x64目录.

找不到***.h

异常信息就不多说了, 多看提示信息.

总之解决方案还是要么先运行vcvars64.bat, 要不搜索该文件的路径, 将其添加到INCLUDE环境变量中.

找不到***.lib

解决方案:

  1. 运行vcvars64.bat
  2. 搜索该文件的路径, 将其添加到LIB环境变量中.

如果你是通过环境变量进行的配置, 那么你就可以不需要在运行native-image之前先运行环境配置脚本了. 但如果visual studio进行更新, cl.exe所在的路径可能会发生变化, 需要重新进行配置.