- Latest
- 22.3
- 22.2
- 22.1
- 22.0
- 21.3
- Dev Build
- User Guides
- Access Environment Variables
- Add Logging to a Native Executable
- Build a Native Executable from a JAR File
- Build and Run Native Executables with JFR
- Build Java Modules into a Native Executable
- Build a Polyglot Native Executable
- Build a Native Shared Library
- Build a Statically Linked or Mostly-Statically Linked Native Executable
- Configure Native Image with the Tracing Agent
- Configure Dynamic Proxies Manually
- Containerise a Native Executable and Run in a Docker Container
- Create a Heap Dump
- Debug Native Executables with GDB
- Include Resources in a Native Executable
- Optimize a Native Executable with PGO
- Use GraalVM Dashboard to Optimize the Size of a Native Executable
- Configure Native Image Using Shared Reachability Metadata
- Use System Properties
Build Java Modules into a Native Executable
GraalVM Native Image supports the Java Platform Module System, introduced in Java 9, which means you can convert a modularized Java application into a native executable.
The native-image
tool accepts the module-related arguments like --module
(-m
), --module-path
(-p
), --add-opens
, --add-exports
(same as for the java
launcher).
When such a module-related argument is used, the native-image
tool itself is used as a module too.
In addition to supporting --add-reads
and --add-modules
, all module related options are considered prior to scanning the module path.
This helps prevent class loading errors and allow for better module introspection at run time.
The command to build a native executable from a Java module is:
native-image [options] --module <module>[/<mainclass>] [options]
Run a Demo #
Follow the steps below to practice building a modular Java application into a native executable. For the demo, you will use a simple HelloWorld Java module gathered with Maven:
├── hello
│ └── Main.java
│ > package hello;
│ >
│ > public class Main {
│ > public static void main(String[] args) {
│ > System.out.println("Hello from Java Module: "
│ > + Main.class.getModule().getName());
│ > }
│ > }
│
└── module-info.java
> module HelloModule {
> exports hello;
> }
-
Download or clone the demos repository and navigate to the directory
native-hello-module
:git clone https://github.com/graalvm/graalvm-demos cd graalvm-demos/native-hello-module
-
Compile and package the project with Maven:
mvn package
-
Test running it on GraalVM’s JDK:
$JAVA_HOME/bin/java --module-path target/HelloModule-1.0-SNAPSHOT.jar --module HelloModule
-
Now build this module into a native executable:
$JAVA_HOME/bin/native-image --module-path target/HelloModule-1.0-SNAPSHOT.jar --module HelloModule
It builds the modular Java application into a native executable called
hellomodule
in the project root directory that you can execute:./hellomodule
Related Documentation #
- Learn more how you can access resources for a Java module at run time.