Skip to content

Commit 9a00b64

Browse files
authored
Apply suggestions from code review
1 parent 64ae44f commit 9a00b64

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

native-image/preserve-package/README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# GraalVM Native Image: Using the -H:Preserve option
1+
# GraalVM Native Image: Using the `-H:Preserve` Option
22

33
Java reflection lets a running program inspect and invoke classes, fields, and
44
methods at runtime. GraalVM Native Image supports many common cases
@@ -16,21 +16,21 @@ shows how to use `-H:Preserve` to keep specific packages available at runtime.
1616
- Maven 3.9+
1717
- SDKMAN! (recommended for installing GraalVM)
1818

19-
Install GraalVM 25 EA with SDKMAN!:
19+
Install GraalVM 25 with SDKMAN!:
2020

2121
```bash
2222
sdk install java 25-graal
2323
sdk use java 25-graal
2424
```
2525

26-
## Get the sources
26+
Clone the example repository:
2727

2828
```bash
2929
git clone https://github.com/graalvm/graalvm-demos
3030
cd graalvm-demos/native-image/preserve-package
3131
```
3232

33-
## What the example does
33+
## How the Example Works
3434

3535
`ReflectionExample` uses command-line arguments to reflectively:
3636
- load a class by name,
@@ -53,13 +53,13 @@ This works on the JVM as long as those classes are on the classpath.
5353

5454
### Run on the JVM
5555

56-
1) Build the JAR:
56+
1. Build the JAR:
5757

5858
```bash
5959
./mvnw package
6060
```
6161

62-
2) Invoke `StringReverser`:
62+
2. Invoke `StringReverser`:
6363

6464
```bash
6565
java -jar target/preserve-package-1.0-SNAPSHOT.jar \
@@ -72,7 +72,7 @@ Expected output:
7272
olleh
7373
```
7474

75-
3) Invoke `StringCapitalizer`:
75+
3. Invoke `StringCapitalizer`:
7676

7777
```bash
7878
java -jar target/preserve-package-1.0-SNAPSHOT.jar \
@@ -85,19 +85,19 @@ Expected output:
8585
HELLO
8686
```
8787

88-
## Build a native image
88+
## Build a Native Image
8989

9090
The project uses the Native Build Tools Maven plugin to drive `native-image`.
9191
The `native-default` profile produces an executable without additional
9292
reflection configuration.
9393

94-
1) Build:
94+
1. Build:
9595

9696
```bash
9797
./mvnw package -Pnative-default
9898
```
9999

100-
2) Run:
100+
2. Run:
101101

102102
```bash
103103
./target/example-default \
@@ -116,7 +116,7 @@ Exception in thread "main" java.lang.ClassNotFoundException: org.graalvm.example
116116
This happens because static analysis did not discover that `StringReverser` (and
117117
`StringCapitalizer`) are used via reflection, so they were not included.
118118

119-
## Identify dynamic access with the Build Report
119+
## Identify Dynamic Access With the Build Report
120120

121121
GraalVM 25 adds an experimental reporting option to help you find dynamic access
122122
before it breaks at runtime. With `-H:+ReportDynamicAccess`, in conjunction with
@@ -133,19 +133,16 @@ The `native-default` profile already enables this feature:
133133
</buildArgs>
134134
```
135135

136-
After building, open the report:
136+
After building, open the report, `target/example-default-build-report.html`, and navigate to the “Dynamic Access” tab to review reflection usage in `ReflectionExample#main`.
137137

138-
- `target/example-default-build-report.html`
139-
- See the “Dynamic Access” tab to review reflection usage in
140-
`ReflectionExample#main`.
141138

142139
The report highlights code that needs to be reviewed to ensure successful
143140
runtime execution of the application. In this application, the classes loaded
144141
via `Class.forName(...)` need to be included in the executable.
145142

146143
![Native Image Build Report - Dynamic Access](build-report.jpeg)
147144

148-
## Fix it with -H:Preserve (experimental)
145+
## Fix It With ‘-H:Preserve’ (Experimental)
149146

150147
GraalVM 25 introduces the experimental `-H:Preserve` option to keep entire
151148
packages, modules, or all classes on the classpath.
@@ -162,13 +159,13 @@ package so `package=org.graalvm.example.action` can be added to the
162159
</buildArgs>
163160
```
164161

165-
1) Build with preserve:
162+
1. Build with preserve:
166163

167164
```bash
168165
./mvnw package -Pnative-preserve
169166
```
170167

171-
2) Run `StringReverser`:
168+
2. Run `StringReverser`:
172169

173170
```bash
174171
./target/example-preserve \
@@ -181,7 +178,7 @@ Expected output:
181178
olleh
182179
```
183180

184-
3) Run `StringCapitalizer`:
181+
3. Run `StringCapitalizer`:
185182

186183
```bash
187184
./target/example-preserve \
@@ -209,7 +206,7 @@ As shown:
209206
- For fine-grained control or for libraries you don’t own, consider JSON
210207
reachability metadata as a complement or alternative.
211208

212-
## Related documentation
209+
## Related Documentation
213210

214211
- Reachability Metadata (Reflection):
215212
https://www.graalvm.org/latest/reference-manual/native-image/metadata/

0 commit comments

Comments
 (0)