Native Image

Overview

A native image is a standalone binary executable or a native shared library compiled from Java code using GraalVM's native-image build tool. This tool compiles Java applications into standalone executables, which include only the necessary application classes, dependencies, and JDK classes required at runtime. The result is a smaller and faster executable that does not require a Java Virtual Machine (JVM) to run. This process is particularly beneficial for Spring Boot applications, as it enhances performance and significantly reduces startup time.

Generating a Native Image

To generate a native image for a Spring Boot application, you can use the GraalVM native build tools. The following command is used to create a native image:

Listing 10.15 Generating native image

mvn clean package spring-boot:build-image

This command cleans the project, packages it, and then uses the Spring Boot plugin to build a native image. The resulting native executable is placed in the application's target folder.

Visual Representation

The following figure illustrates the generated native executable in the application's target folder:

Figure 10.5 Generated native executable in the applications target folder. Figure 10.5 Generated native executable in the applications target folder.