Cloud Native 7 min read

Build Ultra‑Fast Spring Native Apps with GraalVM and UPX

This guide walks you through installing GraalVM, configuring Spring Native with Maven, building a native executable, and compressing it with UPX, demonstrating how to create high‑performance Java microservices in just a few minutes.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Build Ultra‑Fast Spring Native Apps with GraalVM and UPX

Basic Environment

GraalVM is a high‑performance virtual machine that can significantly improve program performance and is well‑suited for microservices.

GraalVM download page
GraalVM download page

Download links (same as a regular JDK installation):

Linux (amd64) – link [1]

Linux (aarch64) – link [2]

MacOS (amd64) – link [3]

Windows (amd64) – link [4]

If you are using macOS Catalina or later, run the following command before installation to avoid a quarantine warning:

<code>sudo xattr -r -d com.apple.quarantine path/to/graalvm/folder/</code>

Getting Started with Spring Native

Create a project from https://start.spring.io [5].

Spring Initializr
Spring Initializr

Add a Maven profile for native‑image builds:

<code>&lt;profiles&gt;
  &lt;profile&gt;
    &lt;id&gt;native-image&lt;/id&gt;
    &lt;build&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.graalvm.nativeimage&lt;/groupId&gt;
          &lt;artifactId&gt;native-image-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;21.0.0&lt;/version&gt;
          &lt;configuration&gt;
            &lt;!-- Specify the entry point of the Main class --&gt;
            &lt;mainClass&gt;com.example.demo.DemoApplication&lt;/mainClass&gt;
          &lt;/configuration&gt;
          &lt;executions&gt;
            &lt;execution&gt;
              &lt;goals&gt;
                &lt;goal&gt;native-image&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;phase&gt;package&lt;/phase&gt;
            &lt;/execution&gt;
          &lt;/executions&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/profile&gt;
&lt;/profiles&gt;</code>

Configure the existing

spring-boot-maven-plugin

to produce an executable JAR:

<code>&lt;plugin&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  &lt;configuration&gt;
    &lt;!-- Use classifier "exec" --&gt;
    &lt;classifier&gt;exec&lt;/classifier&gt;
    &lt;image&gt;
      &lt;builder&gt;paketobuildpacks/builder:tiny&lt;/builder&gt;
      &lt;env&gt;
        &lt;BP_NATIVE_IMAGE&gt;true&lt;/BP_NATIVE_IMAGE&gt;
      &lt;/env&gt;
    &lt;/image&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;</code>

Start Building

Run the Maven command:

<code>mvn -Pnative-image package</code>

Build succeeds in about three minutes.

<code>[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:03 min
[INFO] Finished at: 2021-03-28T12:17:55+08:00
[INFO] Final Memory: 113M/310M
[INFO] ------------------------------------------------------------------------</code>
<code>ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 57M Mar 28 12:17 target/com.example.demo.demoapplication</code>

Run the native binary to test:

<code>target/com.example.demo.demoapplication</code>

Compress Further with UPX

UPX (Ultimate Packer for Executables) is an open‑source tool that compresses executable files.

Install UPX and compress the native image:

<code>brew install upx
upx com.example.demo.demoapplication</code>

The binary size is reduced to about 30% while remaining runnable.

<code>Ultimate Packer for eXecutables
Copyright (C) 1996 - 2020
UPX 3.96    Markus Oberhumer, Laszlo Molnar & John Reiser   Jan 23rd 2020

File size          Ratio  Format   Name
------------------- ------ -------- ------------------------------
60185112 -> 18501648 30.74% macho/amd64 com.example.demo.demoapplication

Packed 1 file.</code>
<code>ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 18M Mar 28 12:17 target/com.example.demo.demoapplication</code>

References

[1] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-linux-amd64-21.0.0.2.tar.gz

[2] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-linux-aarch64-21.0.0.2.tar.gz

[3] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-darwin-amd64-21.0.0.2.tar.gz

[4] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-windows-amd64-21.0.0.2.zip

[5] https://start.spring.io

mavenGraalVMnative-imageSpring NativeUPX
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.