有几种方法可以开始使用 GraalVM。您可以下载适用于您的操作系统的压缩二进制文件,也可以从源代码构建它。出于本书的目的,我们将选择前一个选项。安装步骤如下:
- Navigate to the Downloads page and choose Community Edition. You will be redirected to the GitHub project where the project is hosted. Download the archive that matches with operating system. Please note that the recommended version of GraalVM to use with Quarkus 1.0.0.final is the version 19.2.1. As a matter of fact, the newer 19.3.0 version does not meet the requirements of Quarkus 1.0.0.final.
- Extract the archive to your filesystem.
您应该在提取 GraalVM 的文件夹中具有以下顶级结构:
如您所见,GraalVM 的顶层结构与 JDK 非常相似。在 bin 文件夹中, 您将找到许多实用程序和 JDK 工具的替代品。 值得注意的是,当您在 GraalVM 中使用 java 命令时,它会运行 JVM 和默认编译器,即 Graal。 javac 可用于编译您的代码。除此之外,在利用 GraalVM 的本机和多语言功能时,以下命令是必不可少的:
- js: This command can be executed to run plain JavaScript code if we pass a set of options and the JavaScript filename as an argument.
- node: This command can be used to run Node.js-based applications. It relies on the npm command to install Node.js modules.
- native-image: This command takes your Java class(es) and builds an AOT compiled executable or a shared library. It is not included by default in most recent GraalVM installations and you need the gu tools to install it.
- npm: This is the package manager for Node.js. It puts modules in place so that node can find them and manages dependency conflicts intelligently.
- lli: This is an LLVM bitcode interpreter that can execute LLVM bitcode in a managed environment.
- gu: This tool can be used to install language packs for Python, R, and Ruby, as well as the native-image tool.
我们要做的第一件事是将安装 GraalVM 的路径导出到我们的环境中:
另外,建议完成安装,将 GraalVM 的 bin 文件夹添加到您操作系统的 PATH 中。例如,在 Linux 上,我们将执行以下操作:
或者,您可以通过设置 JAVA_HOME 环境变量来解析到 GraalVM 安装目录,如下所示:
All the preceding environment settings should be added to the script that initializes your shell. For most Linux distributions, this means putting them in the
.bashrc file.
设置 PATH 环境变量后,使用 GraalVM 的启动器检查语言版本非常简单:
属于 GraalVM 中所有语言运行时的可执行文件模拟语言默认运行时的行为。在 PATH 环境变量的开头包含 GraalVM 就足够了,以便使用 GraalVM 运行您的应用程序。