- Latest (GraalVM for JDK 21)
- Dev Build
- GraalVM for JDK 21
- GraalVM for JDK 20
- GraalVM for JDK 17
- GraalVM 22.3
- GraalVM 22.2
- GraalVM 22.1
- GraalVM 22.0
- GraalVM 21.3
- Native Image
- Build Configuration
- Tracing Agent
- Native Image Compatibility and Optimization Guide
- Class Initialization in Native Image
- Static Native Images
- Native Image Options
- Native Image Hosted and Runtime Options
- Native Image C API
- Implementing Native Methods in Java with Native Image
- LLVM Backend for Native Image
- Debug Info Feature
- Points-to Analysis Reports
- Using System Properties in Native Images
- Profile-Guided Optimizations
- Memory Management at Image Run Time
- Generating Heap Dumps from Native Images
- JDK Flight Recorder with Native Image
- JCA Security Services on Native Image
- Dynamic Proxy on Native Image
- Java Native Interface (JNI) on Native Image
- Reflection on Native Image
- Accessing Resources in Native Images
- Logging on Native Image
- URL Protocols on Native Image
- Native Image ARM64 Support
- GraalVM Updater
- Languages References
- Embedding Reference
- Polyglot Programming
Note
This documentation may be out of date. See the latest version.
Static and Mostly Static Images
With GraalVM Native Image you can create static or mostly static images, depending on the purposes.
Static native images are statically linked binaries which can be used without any additional library dependencies.
This makes them easier to distribute and to deploy on slim or distroless container images.
They are created by statically linking against musl-libc, a lightweight, fast and simple libc
implementation.
Mostly static native images statically link against all libraries except libc
.
This approach is ideal for deploying such native images on distroless container images.
Note that it currently only works when linking against glibc
.
Prerequisites #
- Linux AMD64 operating system
- GraalVM distribution for Java 11 with Native Image support
- A 64-bit
musl
toolchain,make
, andconfigure
- The latest
zlib
library
Preparation #
You should get the musl
toolchain first, and then compile and install zlib
into the toolchain.
- Download the
musl
toolchain from musl.cc. This one is recommended. Extract the toolchain to a directory of your choice. This directory will be referred as$TOOLCHAIN_DIR
. - Download the latest
zlib
library sources from here and extract them. This guide useszlib-1.2.11
. - Set the following environment variable:
CC=$TOOLCHAIN_DIR/bin/gcc
- Change into the
zlib
directory, and then run the following commands to compile and installzlib
into the toolchain:./configure --prefix=$TOOLCHAIN_DIR --static make make install
Build Static Native Image #
- First, ensure
$TOOLCHAIN_DIR/bin
is present on yourPATH
variable. To verify this, run:x86_64-linux-musl-gcc
You should get a similar output printed:
x86_64-linux-musl-gcc: fatal error: no input files compilation terminated.
- Build a static native image by using this command:
native-image --static --libc=musl [other arguments] Class
Build Mostly Static Native Image #
As of GraalVM version 20.2, you can build a “mostly static” native image which statically links everything except libc
.
Statically linking all your libraries except glibc
ensures your application has all the libraries it needs to run on any Linux glibc
-based distribution.
To build a mostly-static native image native image, use this command:
native-image -H:+StaticExecutableWithDynamicLibC [other arguments] Class
Note: This currently only works for
glibc
.
Frequently Asked Questions #
What is the recommended base Docker image for deploying a static or mostly static native image? #
A fully static native image gives you the most flexibility to choose a base image - it can run on anything including a FROM scratch
image.
A mostly static native image requires a container image that provides glibc
, but has no additional requirements.
In both cases, choosing the base image mostly depends on what your particular native image needs without having to worry about run-time library dependencies.