You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Demos for building native images, including configurations and setup steps for v
28
28
*[list-files](native-image/list-files/) - Shows how to create a native executable from the command line, and then apply Profile-Guided Optimization (PGO)
29
29
*[native-build-tools](native-image/native-build-tools/) - Contains two Java projects, and shows how to create native executables from those applications using [Maven](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html) and [Gradle](https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html) plugins for GraalVM Native Image
30
30
*[wasm-javac](native-image/wasm-javac/) - Illustrates how to use the new experimental WebAssembly backend in GraalVM to compile `javac` into a Wasm module, which can then run either on the command line or in the browser. [Check out the live demo here](https://graalvm.github.io/graalvm-demos/native-image/wasm-javac/).
31
-
*[preserve-package](native-image/preserve-package/) - Demonstrates how to use the -H:Preserve option to include all classes in a package in a native image instead of using JSON metadata configuration.
31
+
*[preserve-package](native-image/preserve-package/) - Demonstrates how to use the `-H:Preserve` option to include all classes from a package in a native image, eliminating the need for JSON metadata configuration.
32
32
33
33
### Configure
34
34
Demos illustrating how to compile applications with Native Image that use some dynamic Java features including reflection, resource access, and so on.
Copy file name to clipboardExpand all lines: native-image/preserve-package/README.md
+58-47Lines changed: 58 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# Using Native Image `Preserve` Option
2
2
3
-
[Reflection](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/reflect/package-summary.html) is a feature of the Java programming language that enables a running Java program to examine and modify attributes of its classes, interfaces, fields, and methods and GraalVM Native Image provides automatic support for some uses. Native Image uses static analysis to identify what classes, methods, and fields are needed by an application but it may not detect some elements of your application that are accessed using the [Java Reflection API](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/reflect/package-summary.html). Undetected Reflection usage must be declared to the `native-image` tool either in the form of metadata (precomputed in code or as JSON configuration files) or using the `-H:Preserve` option (experimental in GraalVM for JDK 25).
3
+
[Reflection](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/reflect/package-summary.html) is a feature of the Java programming language that enables a running Java program to examine and modify attributes of its classes, interfaces, fields, and methods. GraalVM Native Image automatically supports some uses of reflection. Native Image uses static analysis to identify what classes, methods, and fields are needed by an application, but it may not detect some elements of your application that are accessed using the [Java Reflection API](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/reflect/package-summary.html). You must declare any undetected reflection usage to the `native-image` tool, either as metadata (precomputed in code or as JSON configuration files) or using the `-H:Preserve` option (experimental in GraalVM for JDK 25).
4
4
5
-
The following demonstrates how to declare Reflection configuration using the `-H:Preserve` option.
5
+
This guide demonstrates how to declare reflection configuration using the `-H:Preserve` option.
6
6
7
7
## Preparation
8
8
9
-
1. Download and install the latest GraalVM for JDK 25 (or early access build before 2025-09-16) using [SDKMAN!](https://sdkman.io/).
9
+
1. Download and install the latest GraalVM for JDK 25 (or the early access build before 2025-09-16) using [SDKMAN!](https://sdkman.io/).
10
10
11
11
```shell
12
12
sdk install java 25.ea.29-graal
@@ -16,15 +16,12 @@ The following demonstrates how to declare Reflection configuration using the `-H
We can compile with Native Image specifying the `ReflectionExample` as the main
70
-
entry point. The project [`pom.xml`](pom.xml) uses the [GraalVM Native Build
71
-
Tools](https://graalvm.github.io/native-build-tools/latest/index.html) plugin to
72
-
compile the project using the `native-image` tool when the `native-default`
73
-
profile is specified.
71
+
You can compile the project with Native Image, specifying `ReflectionExample` as the main
72
+
entry point.
73
+
The [_pom.xml_](pom.xml) file uses the [GraalVM Native Build
74
+
Tools](https://graalvm.github.io/native-build-tools/latest/index.html) plugin to compile the project with the `native-image` tool when you selectthe`native-default`
75
+
profile.
76
+
77
+
1. Build a native executable using the `native-default` profile (see the [_pom.xml_](pom.xml) file):
74
78
75
-
1. Build a native executable using the `native-default` profile (see [`pom.xml`](pom.xml)):
76
79
```shell
77
80
./mvnw package -Pnative-default
78
81
```
79
-
4. Run the resulting `example-default` native executable, using the following command:
82
+
83
+
2. Run the resulting `example-default` native executable:
You will see a `ClassNotFoundException` exception, similar to:
89
+
90
+
You will see a `ClassNotFoundException` similar to:
91
+
85
92
```shell
86
93
Exception in thread "main" java.lang.ClassNotFoundException: org.graalvm.example.action.StringReverser
87
94
at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:339)
@@ -92,26 +99,21 @@ profile is specified.
92
99
at org.graalvm.example.ReflectionExample.main(ReflectionExample.java:56)
93
100
at java.base@25/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
94
101
```
95
-
What happened!? Based on its static analysis, the `native-image` tool was unable
96
-
to determine that class `StringReverser` is used by the application and
97
-
therefore did not include it in the native executable.
98
102
99
-
## Native Image using -H:Preserve
103
+
This error occurs because the `native-image` tool's static analysis did not determine that your application uses the `StringReverser` class, and did not include it in the native executable.
104
+
105
+
## Native Image using `-H:Preserve`
106
+
107
+
GraalVM for JDK 25 introduces the `-H:Preserve` option.
108
+
109
+
This option lets you instruct the `native-image` tool to keep entire packages, modules, or all classes on the classpath
100
110
101
-
New in GraalVM for JDK 25 is the `-H:Preserve` option which makes it easy to instruct the
102
-
`native-image` tool to preserve (i.e., keep entirely) packages, modules, and
103
-
even all classes on the classpath (which can result in very large applications).
111
+
<!-- (which can result in very large applications). -->
104
112
105
-
Conveniently in this example, both of the classes that are being used via
106
-
reflection are in the `org.graalvm.example.action` package. We can use
107
-
`-H:Preserve=package` to keep all of the classes in that package in the native
108
-
executable, even though their use is not discoverable through static analysis.
113
+
In this example, both classes used via reflection are in the `org.graalvm.example.action` package. You can use `-H:Preserve=package` to keep all of the classes in that package in the native executable, even if static analysis cannot discover them.
109
114
110
115
Native Image command line arguments can be specified as `<buildArgs>` in the
111
-
`native-maven-plugin` configuration. Note that since the `-H:Preserve` option
112
-
is new and considered experimental in GraalVM for JDK 25, you must also enable
113
-
its use with `-H:+UnlockExperimentalVMOptions`. See the [`pom.xml`](pom.xml) for
114
-
the complete plugin configuration:
116
+
`native-maven-plugin` configuration. As the `-H:Preserve` option is experimental, you must also enable its use with `-H:+UnlockExperimentalVMOptions`. For the complete plugin configuration, see the [_pom.xml_](pom.xml) file:
115
117
116
118
```xml
117
119
<configuration>
@@ -123,33 +125,42 @@ the complete plugin configuration:
123
125
</configuration>
124
126
```
125
127
126
-
1. Build a native executable using the `native-preserve` profile which adds
128
+
1. Build a native executable using the `native-preserve` profile, which adds
127
129
`-H:Preserve=package=org.graalvm.example.action` when running the `native-image`
128
-
tool (see [`pom.xml`](pom.xml)):
130
+
tool (see the [_pom.xml_](pom.xml) file):
131
+
129
132
```shell
130
133
./mvnw package -Pnative-preserve
131
134
```
132
135
133
136
2. Run the new `example-preserve` executable to confirm the previously missing
134
-
`StringReverser` class and all its methods are now included:
137
+
`StringReverser` class and its methods are now included:
0 commit comments