GraalVM Native Image on Windows 10

Steps for Installation and Running Basic Demos



  1. Install the Windows SDK Using Chocolatey:

    C:\> choco install -y windows-sdk-10 kb2919442

    or

    Install the Windows SDK Using Visual Studio Code (see below)

  2. Install Visual Studio 2019 16.8.x (Community will work)

  3. Open the Visual Studio Installer

  4. Under the ‘Installed’ tab, click on the ‘Modify’ button and choose ‘Individual Components

  5. Scroll to the bottom and make certain ‘Windows 10 SDK’ is installed

  6. Follow the GraalVM install instructions for Windows

  7. Verify the GraalVM install

    C:\> java -version

     java version 1.8.0_271
    Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
    Java HotSpot(TM) 64-Bit Server VM GraalVM EE 20.3.0 (build 25.271-b09-jvmci-20.3-b06, mixed mode)
    
  8. Download the native-image module

  9. Install the native-image module

    C:\> gu -L install .\native-image-installable-svm-svmee-java8-windows-amd64-20.3.0.jar

    You may need to use the absolute path to gu, in this example:

    C:\> C:\Progra~1\Java\graalvm-ee-java8-20.3.0\bin\gu install -L path-to-file\native-image-installable-svm-svmee-java8-windows-amd64-20.3.0.jar

  10. Check for installed modules

    C:\> 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
    
  11. Open a Windows command prompt to start a Visual Studio Dev session:

    C:\> “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\vsdevcmd” -arch=amd64

      ******************************************************************
      Visual Studio 2019 Developer Command Prompt v16.8.1
      Copyright (c) 2020 Microsoft Corporation
      ******************************************************************
    
  12. Or launch a Dev command prompt (via Visual Studio 2019):

  13. Create a basic application called HelloWorld.java:

     public class HelloWorld {
       public static void main(String[] args) {
         System.out.println("Hello, GraalVM Native Image!");
       }
     }
    
  14. Compile

    C:\> javac HelloWorld.java

  15. Create a Native Image

    C:\> native-image HelloWorld

     [helloworld:10284]    classlist:     956.55 ms,  0.96 GB
     [helloworld:10284]        (cap):   2,525.43 ms,  0.96 GB
     [helloworld:10284]        setup:   4,266.09 ms,  0.96 GB
     [helloworld:10284]     (clinit):     125.53 ms,  1.19 GB
     [helloworld:10284]   (typeflow):   3,513.99 ms,  1.19 GB
     [helloworld:10284]    (objects):   3,226.40 ms,  1.19 GB
     [helloworld:10284]   (features):     159.53 ms,  1.19 GB
     [helloworld:10284]     analysis:   7,136.97 ms,  1.19 GB
     [helloworld:10284]     universe:     249.78 ms,  1.19 GB
     [helloworld:10284]      (parse):     593.14 ms,  1.67 GB
     [helloworld:10284]     (inline):     818.67 ms,  1.67 GB
     [helloworld:10284]    (compile):   7,087.49 ms,  3.19 GB
     [helloworld:10284]      compile:   8,959.29 ms,  3.19 GB
     [helloworld:10284]        image:     742.99 ms,  3.19 GB
     [helloworld:10284]        write:     236.82 ms,  3.19 GB
     [helloworld:10284]      [total]:  22,670.83 ms,  3.19 GB
    
  16. Run the Native Image version

    C:\> helloworld
    Hello, GraalVM Native Image!

  17. Measure Startup Time (using 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
    


Micronaut Example



Install Micronaut using Chocolatey:

C:\> choco install -y micronaut

or

Install using instructions here.

Follow the Micronaut/GraalVM tutorial here.


Polyglot Example



  1. Create a file called PrettyPrintJSON.java:
 import java.io.*;
 import java.util.stream.*;
 import org.graalvm.polyglot.*;

 public class PrettyPrintJSON {
   public static void main(String[] args) throws java.io.IOException {
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String input = reader.lines()
     .collect(Collectors.joining(System.lineSeparator()));
     try (Context context = Context.create("js")) {
       Value parse = context.eval("js", "JSON.parse");
       Value stringify = context.eval("js", "JSON.stringify");
       Value result = stringify.execute(parse.execute(input), null, 2);
       System.out.println(result.asString());
      }
     }
 }
  1. Compile the code:

    C:\> javac PrettyPrintJSON.java

  2. Create a Native Image:

    C:\> native-image –language:js –initialize-at-build-time PrettyPrintJSON

  3. With Windows, when you are ready to complete the input, press the Enter key and then press Ctrl+Z and then Enter again to complete the input (see below).

  4. Run the application:

    C:\> prettyprintjson

    {"GraalVM":{"description":"Language Abstraction Platform","supports":["combining languages","embedding languages","creating native images"],"languages": 	["Java","JavaScript","Node.js", "Python", "Ruby","R","LLVM"]}} 
    <enter> CTRL-Z <enter>
    
    {
     "GraalVM": {
      "description": "Language Abstraction Platform",
       "supports": [
         "combining languages",
         "embedding languages",
         "creating native images"
      ],
       "languages": [
          "Java",
          "JavaScript",
          "Node.js",
          "Python",
          "Ruby",
          "R",
          "LLVM"
       ]
      }
    }