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: native-image/preserve-package/README.md
+25-29Lines changed: 25 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,9 @@
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. 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).
3
+
Reflection 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.
4
+
GraalVM Native Image automatically supports some uses of reflection.
5
+
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/24/docs/api/java.base/java/lang/reflect/package-summary.html).
6
+
You must declare any undetected reflection usage to the `native-image` tool, either as metadata ([precomputed in code or as JSON configuration files](https://www.graalvm.org/latest/reference-manual/native-image/metadata/)) or using the `-H:Preserve` option (experimental in GraalVM for JDK 25).
4
7
5
8
This guide demonstrates how to declare reflection configuration using the `-H:Preserve` option.
6
9
@@ -12,18 +15,16 @@ This guide demonstrates how to declare reflection configuration using the `-H:Pr
12
15
sdk install java 25.ea.29-graal
13
16
```
14
17
15
-
2. Download or clone this repository and navigate into the `native-image/preserve-package` directory:
18
+
2. Download or clone this repository, and navigate into the `native-image/preserve-package` directory:
The `ReflectionExample` class uses command line argument values to
25
-
reflectively create an instance of a class and invoke a method with a
26
-
provided argument. The core code is:
27
+
The `ReflectionExample` class uses command line argument values to reflectively create an instance of a class and invoke a method with a provided argument. The core code is:
27
28
28
29
```java
29
30
Class<?> clazz = Class.forName(className);
@@ -36,11 +37,10 @@ This approach works on the JVM as long as the required classes and methods are a
36
37
1. Compile the application and create a JAR file using Maven:
37
38
38
39
```shell
39
-
./mvnw package
40
+
./mvnw package
40
41
```
41
42
42
-
2. Run `ReflectionExample` (the JAR entry point) to invoke the
43
-
`StringReverser` action:
43
+
2. Run `ReflectionExample` (the JAR entry point) to invoke the `StringReverser` action:
@@ -68,16 +68,13 @@ This approach works on the JVM as long as the required classes and methods are a
68
68
69
69
## GraalVM Native Image
70
70
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.
71
+
You can compile the project with Native Image, specifying `ReflectionExample` as the main entry point.
72
+
The [_pom.xml_](pom.xml) file uses the [Native Build Tools Maven plugin](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html) to compile the project with the `native-image` tool.
76
73
77
74
1. Build a native executable using the `native-default` profile (see the [_pom.xml_](pom.xml) file):
78
75
79
76
```shell
80
-
./mvnw package -Pnative-default
77
+
./mvnw package -Pnative-default
81
78
```
82
79
83
80
2. Run the resulting `example-default` native executable:
@@ -102,18 +99,20 @@ profile.
102
99
103
100
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
101
105
-
## Native Image using `-H:Preserve`
102
+
## Native Image Using `-H:Preserve`
106
103
107
104
GraalVM for JDK 25 introduces the `-H:Preserve` option.
108
105
109
-
This option lets you instruct the `native-image` tool to keep entire packages, modules, or all classes on the classpath
106
+
This option lets you instruct the `native-image` tool to keep entire packages, modules, or all classes on the classpath.
110
107
111
-
<!-- (which can result in very large applications). -->
108
+
<!-- This can result in larger application size. -->
112
109
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.
110
+
In this example, both classes used via reflection are in the `org.graalvm.example.action` package.
111
+
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.
114
112
115
-
Native Image command line arguments can be specified as `<buildArgs>` in the
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:
113
+
Native Image command line arguments can be specified as `<buildArgs>` in the `native-maven-plugin` configuration.
114
+
Since the `-H:Preserve` option is experimental, you must also enable its use with `-H:+UnlockExperimentalVMOptions`.
115
+
For the complete plugin configuration, see the [_pom.xml_](pom.xml) file:
117
116
118
117
```xml
119
118
<configuration>
@@ -125,16 +124,13 @@ Native Image command line arguments can be specified as `<buildArgs>` in the
125
124
</configuration>
126
125
```
127
126
128
-
1. Build a native executable using the `native-preserve` profile, which adds
129
-
`-H:Preserve=package=org.graalvm.example.action` when running the `native-image`
130
-
tool (see the [_pom.xml_](pom.xml) file):
127
+
1. Build a native executable using the `native-preserve` profile, which adds `-H:Preserve=package=org.graalvm.example.action` when running the `native-image` tool:
131
128
132
129
```shell
133
-
./mvnw package -Pnative-preserve
130
+
./mvnw package -Pnative-preserve
134
131
```
135
132
136
-
2. Run the new `example-preserve` executable to confirm the previously missing
137
-
`StringReverser` class and its methods are now included:
133
+
2. Run the new `example-preserve` executable to confirm the previously missing `StringReverser` class and its methods are now included:
138
134
139
135
```shell
140
136
./target/example-preserve \
@@ -165,5 +161,5 @@ As demonstrated, `-H:Preserve` provides an easy way to ensure that Native Image
0 commit comments