GraalVM Demo — Creating a Native (Windows) Executable

First, Install GraalVM.  Download from https://www.graalvm.org/downloads/ and follow the installtion directions.

C:\tmp>java --version
java 23 2024-09-17
Java(TM) SE Runtime Environment Oracle GraalVM 23+37.1 (build 23+37-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 23+37.1 (build 23+37-jvmci-b01,
 mixed mode, sharing)

C:\tmp>javac --version
javac 23

C:\tmp>notepad Hello.java

C:\tmp>type Hello.java
public class Hello {
    public static void main ( String[] args ) {
        System.out.println( "Hello!" );
    }
}

C:\tmp>javac Hello.java

C:\tmp>java Hello
Hello!

C:\tmp>native-image --enable-sbom=export Hello
================================================================================
GraalVM Native Image: Generating 'hello' (executable)...
================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
--------------------------------------------------------------------------------
[1/8] Initializing...                                            (4.4s @ 0.12GB)
 Java version: 23+37, vendor version: Oracle GraalVM 23+37.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: cl.exe (microsoft, x64, 19.41.34120)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 1 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
--------------------------------------------------------------------------------
Build resources:
 - 11.87GB of memory (75.6% of 15.71GB system memory, determined at start)
 - 20 thread(s) (100.0% of 20 available processor(s), determined at start)
Warning: We could not find any version info for the following classes:
Hello
[2/8] Performing analysis...  [*****]                            (2.4s @ 0.19GB)
    1,982 reachable types   (58.1% of    3,414 total)
    1,770 reachable fields  (37.6% of    4,711 total)
    8,572 reachable methods (35.3% of   24,290 total)
      699 types,     7 fields, and    84 methods registered for reflection
       53 types,    30 fields, and    48 methods registered for JNI access
        1 native library: version
        1 native library: version
[3/8] Building universe...                                       (0.5s @ 0.20GB)
[4/8] Parsing methods...      [*]                                (0.6s @ 0.24GB)
[5/8] Inlining methods...     [***]                              (0.4s @ 0.32GB)
[6/8] Compiling methods...    [***]                              (5.7s @ 0.46GB)
[7/8] Laying out methods...   [*]                                (0.7s @ 0.49GB)
[8/8] Creating image...       [*]                                (0.6s @ 0.52GB)
  3.04MB (46.78%) for code area:     3,890 compilation units
  3.38MB (51.86%) for image heap:   52,647 objects and 52 resources
 90.89kB ( 1.36%) for other data
  6.51MB in total
--------------------------------------------------------------------------------
Top 10 origins of code area:                Top 10 object types in image heap:
   1.44MB svm.jar (Native Image)            685.75kB byte[] for code metadata
   1.32MB java.base                         672.64kB byte[] for java.lang.String
  86.07kB com.oracle.svm.svm_enterprise     389.26kB heap alignment
  35.36kB org.graalvm.nativeimage.base      356.65kB java.lang.String
  35.14kB jdk.proxy2                        335.68kB java.lang.Class
  27.65kB jdk.graal.compiler                147.03kB java.util.HashMap$Node
  26.22kB jdk.proxy1                        114.52kB char[]
  22.94kB org.graalvm.collections            92.91kB com.oracle.svm.core.hub.DynamicHubCompanion
  20.91kB jdk.internal.vm.ci                 84.84kB java.lang.Object[]
   8.13kB jdk.proxy3                         81.01kB byte[] for reflection metadata
  621.00B for 3 more packages               495.72kB for 510 more object types
                 Use '--emit build-report' to create a report with more details.
--------------------------------------------------------------------------------
Security report:
 - Binary includes Java deserialization.
 - CycloneDX SBOM with 5 component(s) is exported as JSON (see build artifacts).
--------------------------------------------------------------------------------
Recommendations:
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
--------------------------------------------------------------------------------
        1.0s (5.6% of total time) in 349 GCs | Peak RSS: 1.06GB | CPU load: 3.69
--------------------------------------------------------------------------------
Build artifacts:
 C:\tmp\hello.exe (executable)
================================================================================
Finished generating 'hello' in 16.7s.

C:\tmp>dir
 Volume in drive C is Windows
 Volume Serial Number is A2D2-438D

 Directory of C:\tmp

10/04/2024  06:04 PM    <DIR>          .
10/04/2024  05:26 PM               410 Hello.class
10/04/2024  06:04 PM         6,823,936 hello.exe
10/04/2024  05:25 PM               115 Hello.java
10/04/2024  06:04 PM             1,323 hello.sbom.json
               4 File(s)      6,825,784 bytes
               1 Dir(s)  527,840,772,096 bytes free

C:\tmp>jq <hello.sbom.json
{
  "bomFormat": "CycloneDX",
  "specVersion": "1.4",
  "version": 1,
  "components": [
    {
      "type": "library",
      "group": "com.oracle.svm",
      "name": "svm",
      "version": "23+37",
      "properties": [
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:oracle:graalvm-native-image:23+37:*:*:*:*:*:*:*"
        }
      ]
    },
    {
      "type": "library",
      "group": "jdk.graal",
      "name": "compiler",
      "version": "23+37",
      "properties": [
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:compiler:compiler:23+37:*:*:*:*:*:*:*"
        },
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:graal:compiler:23+37:*:*:*:*:*:*:*"
        }
      ]
    },
    {
      "type": "library",
      "group": "org.graalvm",
      "name": "collections",
      "version": "23+37",
      "properties": [
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:collections:collections:23+37:*:*:*:*:*:*:*"
        },
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:graalvm:collections:23+37:*:*:*:*:*:*:*"
        }
      ]
    },
    {
      "type": "library",
      "group": "org.graalvm",
      "name": "nativeimage",
      "version": "23+37",
      "properties": [
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:graalvm:nativeimage:23+37:*:*:*:*:*:*:*"
        },
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:nativeimage:nativeimage:23+37:*:*:*:*:*:*:*"
        }
      ]
    },
    {
      "type": "library",
      "group": "org.graalvm",
      "name": "word",
      "version": "23+37",
      "properties": [
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:graalvm:word:23+37:*:*:*:*:*:*:*"
        },
        {
          "name": "syft:cpe23",
          "value": "cpe:2.3:a:word:word:23+37:*:*:*:*:*:*:*"
        }
      ]
    }
  ],
  "serialNumber": "urn:uuid:e6443a8a-c006-32f7-89f1-e1067dc7e278"
}

C:\tmp>.\hello
Hello!

C:\tmp>