Using GraalVM and Native Image on Windows

Olga Gupalo
graalvm
Published in
4 min readSep 16, 2021

--

Co-authored by

, Oracle

Note: Beginning with GraalVM for JDK 17 / 20, the native-image tool looks for a Visual Studio installation, and (if it finds one) automatically sets your build environment. Therefore, you can run native-imagein a Command Prompt or a PowerShell without any additional installation.

This blog post was updated to be compatible with GraalVM for JDK 21 and later. Installation instructions for older GraalVM versions are available on the website.

Using GraalVM and Native Image on Windows

This post will help anyone using Windows install the high-performance GraalVM JDK with its Native Image tooling.

We’ve seen an increasing number of Windows users expressing interest in using GraalVM Native Image, which is an ahead-of-time (AOT) compilation tool that packages Java bytecode to a standalone executable. This executable is a native application like any other on the user’s system, and typically achieves a much faster startup time while occupying a smaller footprint.

GraalVM installation on Windows is quick and easy. There are just a few prerequisites for using Native Image. Let’s go step by step.

Part 1: Install GraalVM

  1. Go to graalvm.org/downloads, select a Java version and download.
  2. Change directory to the location where you want to install GraalVM, then move the .zip file to it.
  3. Uncompress the file.
  4. Next, you should configure the runtime environment, being mindful that there may be multiple JDKs installed on the machine. Open a Windows command prompt and do the following:
    - Set the value of the PATH environment variable to the GraalVM bin directory:
    setx /M PATH “C:\Progra~1\Java\<graalvm install dir>\bin;%PATH%”
    - Set the value of the JAVA_HOME environment variable to the installation directory:
    setx /M JAVA_HOME “C:\Progra~1\Java\<graalvm install dir>”

5. To verify that GraalVM is installed successfully, restart the command prompt and execute java -version.

Native Image is shipped with GraalVM, but on Windows, it requires Visual Studio 2022 version 17.1.0 or later, and Microsoft Visual C++(MSVC). There are two installation options:

  • Install the Visual Studio Build Tools with the Windows 11 SDK
  • Install Visual Studio with the Windows 11 SDK

Part 2: Install Visual Studio Build Tools and Windows 11 SDK

Visual Studio

  1. Download the Visual Studio Build Tools 2022 (C development environment) version 17.1.0 or later from visualstudio.microsoft.com.
  2. Start the Visual Studio Build Tools installation by clicking on the .exe file, and then click Continue:
Start Visual Studio Build Tools Installation

3. Select the Desktop development with C++ check box in the main window. Also, on the right side under Installation Details, select Windows 11 SDK, and click Install.

Desktop development with C++ and Windows 11 SDK

4. After the installation completes, reboot your system.

Windows 11 SDK:

Next, if you have Visual Studio 2022 installed, you will need to ensure Windows 11 SDK is available too:

  1. Open the Visual Studio Installer:
Visual Studio Installer

2. Under the Installed tab, click Modify and choose Individual Components:

Visual Studio Installer

3. Then scroll to the bottom and check if Windows 11 SDK is installed and confirm the build tools are installed.

Now you can start using GraalVM Native Image.

Part 3. Start Using Native Image on Windows

Native Image sets up build environments automatically on Windows if it can find an appropriate Visual Studio installation in a known location.

Since you have Windows 11 SDK and Visual Studio tooling installed, you can now run the native-image utility in a Command Prompt (cmd) or a PowerShell (pwsh). For example, check the native-image version:

C:\> native-image.cmd - version

Alternatively, you can launch a Dev command prompt from the Visual Studio 2022 interface.

Now let’s experiment with Native Image and see what it can do!

  1. Save this simple code to a HelloWorld.java file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, GraalVM Native Image!");
}
}

2. Compile and run it on the JVM:

C:\> javac HelloWorld
C:\> java HelloWorld

3. Now compile the class into a native executable:

C:\> native-image HelloWorld

This will produce an executable file, helloworld, in the working directory.

4. Now execute it:

C:\> helloworld    
Hello, GraalVM Native Image!

Compare the startup times when running on the JVM (step 2) versus when executing a binary produced by native-image (step 4). Or measure the time in PowerShell:

C:\> Measure-Command {".\helloworld"}

Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 3
Ticks : 38000
TotalDays : 4.39814814814815E-08
TotalHours : 1.05555555555556E-06
TotalMinutes : 6.33333333333333E-05
TotalSeconds : 0.0038
TotalMilliseconds : 3.8

Summary
Installing GraalVM on Windows is easy! Just remember, there are certain requirements in order to use Native Image. With Native Image, you can take full advantage of vastly increased startup time and smaller footprint for compiling your Java applications ahead-of-time. This guide was written for Windows 11, but should be valid for Windows 8 and 10 as well.

--

--