Skip to content

Commit 7b49365

Browse files
committed
[GR-43129] [GR-43524] Documentation: Native Image User Guides updates.
PullRequest: graal/13525
2 parents 2617bc9 + 92ed601 commit 7b49365

8 files changed

+166
-87
lines changed

docs/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ The following table lists production-ready and experimental features in GraalVM
116116
| Python | experimental | not available | experimental | not available | not available |
117117
| Ruby | experimental | experimental | experimental | experimental | not available |
118118
| R | experimental | not available | experimental | not available | not available |
119-
| WebAssembly | experimental | experimental | experimental | experimental | experimental |
119+
| WebAssembly | experimental | experimental | experimental | not available | experimental |
120120

121121
## What to Read Next
122122

docs/reference-manual/native-image/README.md

+2-60
Original file line numberDiff line numberDiff line change
@@ -144,67 +144,9 @@ To build a native executable from a JAR file in the current working directory, u
144144
native-image [options] -jar jarfile [imagename]
145145
```
146146

147-
1. Prepare the application.
147+
The default behavior of `native-image` is aligned with the `java` command which means you can pass the `-jar`, `-cp`, `-m` options to build with Native Image as you would normally do with `java`. For example, `java -jar App.jar someArgument` becomes `native-image -jar App.jar` and `./App someArgument`.
148148

149-
- Create a new Java project named "App", for example in your favorite IDE or from your terminal, with the following structure:
150-
151-
```shell
152-
| src
153-
| --com/
154-
| -- example
155-
| -- App.java
156-
```
157-
158-
- Add the following Java code into the _src/com/example/App.java_ file:
159-
160-
```java
161-
package com.example;
162-
163-
public class App {
164-
165-
public static void main(String[] args) {
166-
String str = "Native Image is awesome";
167-
String reversed = reverseString(str);
168-
System.out.println("The reversed string is: " + reversed);
169-
}
170-
171-
public static String reverseString(String str) {
172-
if (str.isEmpty())
173-
return str;
174-
return reverseString(str.substring(1)) + str.charAt(0);
175-
}
176-
}
177-
```
178-
This is a small Java application that reverses a String using recursion.
179-
180-
2. Compile the application:
181-
182-
```shell
183-
javac -d build src/com/example/App.java
184-
```
185-
This produces the file _App.class_ in the _build/com/example_ directory.
186-
187-
3. Create a runnable JAR file:
188-
189-
```shell
190-
jar --create --file App.jar --main-class com.example.App -C build .
191-
```
192-
It will generate a runnable JAR file, named `App.jar`, in the root directory:
193-
To view its contents, type `jar tf App.jar`.
194-
195-
4. Create a native executable:
196-
197-
```
198-
native-image -jar App.jar
199-
```
200-
It will produce a native executable in the project root directory.
201-
5. Run the native executable:
202-
203-
```shell
204-
./App
205-
```
206-
207-
The `native-image` tool can provide the class path for all classes using the familiar option from the java launcher: `-cp`, followed by a list of directories or JAR files, separated by `:` on Linux and macOS platforms, or `;` on Windows. The name of the class containing the `main` method is the last argument, or you can use the `-jar` option and provide a JAR file that specifies the `main` method in its manifest.
149+
[Follow this guide](guides/build-native-executable-from-jar.md) to build a native executable from a JAR file.
208150

209151
### From a Module
210152

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
layout: ni-docs
3+
toc_group: how-to-guides
4+
link_title: Build a Native Executable from a JAR File
5+
permalink: /reference-manual/native-image/guides/build-native-executable-from-jar/
6+
---
7+
8+
# Build a Native Executable from a JAR File
9+
10+
You can build a native executable from a class file, from a JAR file, or from a module. This guide demonstrates how to build a native executable from a JAR file.
11+
12+
To build a native executable from a JAR file in the current working directory, use the following command:
13+
```shell
14+
native-image [options] -jar jarfile [executable name]
15+
```
16+
17+
1. Prepare the application.
18+
19+
- Create a new Java project named "App", for example in your favorite IDE or from your terminal, with the following structure:
20+
21+
```shell
22+
| src
23+
| --com/
24+
| -- example
25+
| -- App.java
26+
```
27+
28+
- Add the following Java code to the _src/com/example/App.java_ file:
29+
30+
```java
31+
package com.example;
32+
33+
public class App {
34+
35+
public static void main(String[] args) {
36+
String str = "Native Image is awesome";
37+
String reversed = reverseString(str);
38+
System.out.println("The reversed string is: " + reversed);
39+
}
40+
41+
public static String reverseString(String str) {
42+
if (str.isEmpty())
43+
return str;
44+
return reverseString(str.substring(1)) + str.charAt(0);
45+
}
46+
}
47+
```
48+
This is a small Java application that reverses a String using recursion.
49+
50+
2. Compile the application:
51+
52+
```shell
53+
javac -d build src/com/example/App.java
54+
```
55+
This produces the file _App.class_ in the _build/com/example_ directory.
56+
57+
3. Create a runnable JAR file:
58+
59+
```shell
60+
jar --create --file App.jar --main-class com.example.App -C build .
61+
```
62+
It will generate a runnable JAR file, named _App.jar_, in the project root directory:
63+
To view its contents, run the command `jar tf App.jar`.
64+
65+
4. Create a native executable:
66+
67+
```
68+
native-image -jar App.jar
69+
```
70+
It will produce a native executable in the project root directory.
71+
5. Run the native executable:
72+
73+
```shell
74+
./App
75+
```
76+
77+
The default behavior of `native-image` is aligned with the `java` command which means you can pass the `-jar`, `-cp`, `-m` options to build with Native Image as you would normally do with `java`. For example, `java -jar App.jar someArgument` becomes `native-image -jar App.jar` and `./App someArgument`.
78+
79+
### Related Documentation
80+
81+
* [GraalVM Native Image Quick Start](https://luna.oracle.com/lab/47dafec8-4095-4fba-8313-dad43a64dee4)
82+
* [Build Java Modules into a Native Executable](build-java-module-app-aot.md)

docs/reference-manual/native-image/guides/build-with-reflection.md

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
---
22
layout: ni-docs
33
toc_group: how-to-guides
4-
link_title: Build with Reflection
5-
permalink: /reference-manual/native-image/guides/build-with-reflection/
4+
link_title: Configure Native Image with the Tracing Agent
5+
permalink: /reference-manual/native-image/guides/configure-with-tracing-agent/
6+
redirect_from:
7+
- /reference-manual/native-image/guides/build-with-reflection/
68
---
79

8-
# Build a Native Executable with Reflection
10+
# Configure Native Image with the Tracing Agent
911

10-
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.
12+
To build a native executable for a Java application that uses Java reflection, dynamic proxy objects, JNI, or class path resources, you should either provide the `native-image` tool with JSON-formatted configuration files or pre-compute metadata in the code.
1113

12-
The `native-image` utility provides partial support for reflection. It uses static analysis to detect the elements of your application that are accessed using the Java Reflection API. However, because the analysis is static, it cannot always completely predict all usages of the API when the program runs. In these cases, you must provide a configuration file to the native-image utility to specify the program elements that use the API.
14+
You can create configuration file(s) by hand, but a more convenient approach is to generate the configuration using the Tracing agent (from now on, the agent).
15+
This guide demonstrates how to configure `native-image` with the agent. The agent generates the configuration for you automatically when you run an application on a JVM.
1316

17+
To learn how to build a native executable with the metadata pre-computed in the code, [follow this guide](use-reachability-metadata-repository-gradle.md).
18+
19+
The example application in this guide uses Java reflection. The `native-image` tool only partially detects application elements that are accessed using the Java Reflection API. So, you need to provide it with details about reflectively accessed classes, methods, and fields.
1420
## Example with No Configuration
21+
1522
The following application demonstrates the use of Java reflection.
1623

1724
1. Save the following source code in a file named _ReflectionExample.java_:
@@ -78,16 +85,15 @@ The following application demonstrates the use of Java reflection.
7885
is used by the application and therefore did not include it in the native executable.
7986

8087
## Example with Configuration
81-
To build a native executable containing references to the classes and methods that are accessed via reflection, provide the `native-image` utility with a configuration file that specifies the classes and corresponding methods. (For more information about configuration files, see [Reflection Use in Native Images](../Reflection.md).) You can create this file by hand, but a more convenient approach is to generate the configuration using the tracing agent. The agent generates the configuration for you automatically when you run your application (for more information, see [Assisted Configuration with Tracing Agent](../AutomaticMetadataCollection.md#tracing-agent).
8288

83-
The following steps demonstrate how to use the javaagent tool, and its output, to create a native executable that relies on reflection.
89+
The following steps demonstrate how to use the agent, and its output, to create a native executable that relies on reflection and requires configuration.
8490

85-
1. Create a directory `META-INF/native-image` in the working directory:
91+
1. Create a directory named _META-INF/native-image_ in the working directory:
8692
```shell
8793
mkdir -p META-INF/native-image
8894
```
8995

90-
2. Run the application with the tracing agent enabled, as follows:
96+
2. Run the application with the agent enabled, as follows:
9197
```shell
9298
$JAVA_HOME/bin/java -agentlib:native-image-agent=config-output-dir=META-INF/native-image ReflectionExample StringReverser reverse "hello"
9399
```
@@ -140,7 +146,7 @@ The following steps demonstrate how to use the javaagent tool, and its output, t
140146
]
141147
```
142148

143-
6. Rebuild a native executable and run the resulting file.
149+
6. Rebuild the native executable and run it.
144150
```shell
145151
$JAVA_HOME/bin/native-image ReflectionExample
146152
./reflectionexample StringCapitalizer capitalize "hello"
@@ -150,6 +156,6 @@ The following steps demonstrate how to use the javaagent tool, and its output, t
150156

151157
### Related Documentation
152158

159+
* [Assisted Configuration with Tracing Agent](../AutomaticMetadataCollection.md#tracing-agent)
153160
* [Reachability Metadata: Reflection](../ReachabilityMetadata.md#reflection)
154-
* [Assisted Configuration with Tracing Agent](../AutomaticMetadataCollection.md#tracing-agent)
155161
* [java.lang.reflect Javadoc](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html)

docs/reference-manual/native-image/guides/debug-native-executables-with-gdb.md

+50-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,61 @@ This will enable source-level debugging, and the debugger (GDB) then correlates
2828
Follow the steps to test debugging a native executable with GDB. The below workflow is know to work on Linux OS with GDB 10.1.
2929

3030
1. Save the following code to the file named _GDBDemo.java_.
31+
```java
32+
public class GDBDemo {
33+
static long fieldUsed = 1000;
34+
35+
public static void main( String[] args ) {
36+
if (args.length > 0) {
37+
int n = -1;
38+
try {
39+
n = Integer.parseInt(args[0]);
40+
} catch (NumberFormatException ex) {
41+
System.out.println(args[0] + " is not a number!");
42+
}
43+
if (n < 0) {
44+
System.out.println(args[0] + " is negative.");
45+
}
46+
double f = factorial(n);
47+
System.out.println(n + "! = " + f);
48+
}
49+
50+
if (false)
51+
neverCalledMethod();
52+
53+
StringBuilder text = new StringBuilder();
54+
text.append("Hello World from GraalVM Native Image and GDB in Java.\n");
55+
System.out.println(text.toString());
56+
}
57+
58+
static void neverCalledMethod() {
59+
System.out.println("This method is unreachable and will not be included in the native executable.");
60+
}
61+
62+
static double factorial(int n) {
63+
if (n == 0) {
64+
return 1;
65+
}
66+
if (n >= fieldUsed) {
67+
return Double.POSITIVE_INFINITY;
68+
}
69+
double f = 1;
70+
while (n > 1) {
71+
f *= n--;
72+
}
73+
return f;
74+
}
75+
}
76+
77+
```
3178

3279
2. Compile it and generate a native executable with debug information:
3380

3481
```shell
35-
$JAVA_HOME/bin/javac JFRDemo.java
82+
$JAVA_HOME/bin/javac GDBDemo.java
3683
```
3784
```shell
38-
native-image -g -O0 JFRDemo
85+
native-image -g -O0 GDBDemo
3986
```
4087
The `-g` option instructs `native-image` to generate debug information. The resulting native executable will contain debug records in a format GDB understands.
4188

@@ -44,7 +91,7 @@ Follow the steps to test debugging a native executable with GDB. The below workf
4491
3. Launch the debugger and run your native executable:
4592

4693
```shell
47-
gdb ./jfrdemo
94+
gdb ./gdbdemo
4895
```
4996
The `gdb` prompt will open.
5097

docs/reference-manual/native-image/guides/guides.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
---
2-
layout: ni-docs
2+
layout: ni-docs-landing
33
toc_group: how-to-guides
4-
link_title: How-to Guides
5-
permalink: /reference-manual/native-image/guides/
4+
link_title: User Guides
5+
permalink: /native-image/guides/
6+
redirect_from: /reference-manual/native-image/guides/
67
---
78

8-
# Native Image How-to Guides
9+
# Native Image User Guides
910

10-
These guides are to help developers get started with GraalVM Native Image, acquaint them with the available features, and describe potential usage scenarios.
11+
These guides help developers get started with GraalVM Native Image, acquaint them with available features, and describe potential usage scenarios.
1112
Here you will learn how to:
1213

1314
- [Access Environment Variables](access-environment-variables.md)
1415
- [Add Logging to a Native Executable](add-logging-to-native-executable.md)
16+
- [Build a Native Executable from a JAR File](build-native-executable-from-jar.md)
1517
- [Build and Run Native Executables with JFR](build-and-run-native-executable-with-jfr.md)
1618
- [Build Java Modules into a Native Executable](build-java-module-app-aot.md)
1719
- [Build a Native Shared Library](build-native-shared-library.md)
1820
- [Build a Polyglot Native Executable (Java and JavaScript)](build-polyglot-native-executable.md)
1921
- [Build a Static or Mostly-Static Native Executable](build-static-and-mostly-static-executable.md)
20-
- [Build a Native Executable with Reflection](build-with-reflection.md)
22+
- [Configure Native Image with the Tracing Agent](build-with-reflection.md)
2123
- [Containerise a Native Executable and Run in a Docker Container](containerise-native-executable-with-docker.md)
2224
- [Create a Heap Dump from a Native Executable](create-heap-dump-from-native-executable.md)
2325
- [Debug Native Executables with GDB](debug-native-executables-with-gdb.md)

docs/reference-manual/native-image/guides/optimize-native-executable-with-pgo.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,4 @@ With PGO you "train" your application for specific workloads and significantly i
186186

187187
### Related Documentation
188188

189-
- [Improving performance of GraalVM native images with profile-guided optimizations](https://medium.com/graalvm/improving-performance-of-graalvm-native-images-with-profile-guided-optimizations-9c431a834edb)
189+
- [Optimize Cloud Native Java Apps with GraalVM Enterprise PGO](https://luna.oracle.com/lab/3f0b7c86-6105-4b7a-9a3b-eb73b251a1aa)

docs/reference-manual/native-image/guides/use-reachability-metadata-repository-gradle.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
22
layout: ni-docs
33
toc_group: how-to-guides
4-
link_title: Use Shared Reachability Metadata with Native Image Gradle Plugin
4+
link_title: Configure Native Image Using Shared Reachability Metadata
55
permalink: /reference-manual/native-image/guides/use-reachability-metadata-repository-gradle/
66
---
77

8-
# Use Shared Reachability Metadata with Native Image Gradle Plugin
8+
# Configure Native Image Using Shared Reachability Metadata
99

1010
With the Gradle plugin for GraalVM Native Image you can easily build a native executable from a Java application. The plugin is provided as part of the [Native Build Tools project](https://graalvm.github.io/native-build-tools/latest/index.html) and uses the [Gradle build tool](https://gradle.org/).
11-
If the application does not load dynamically any classes at run time, then your workflow is just one command: `./gradlew nativeRun`.
11+
If the application does not dynamically load any classes at run time, then your workflow is just one command: `./gradlew nativeRun`.
1212

13-
In the real-world scenario, your application will, most likely, call either Java Reflection, Dynamic Proxy objects, or call some native code, or access classpath resources - the dynamic features which the `native-image` tool must be aware of at build time, and provided in the form of [metadata](../ReachabilityMetadata.md).
13+
In the real-world, your application will, most likely, call either Java Reflection, Dynamic Proxy objects, or call some native code, or access resources on the class path - dynamic features that the `native-image` tool must be aware of at build time, and provided in the form of [metadata](../ReachabilityMetadata.md).
1414
Native Image loads classes dynamically at build time, and not at run time.
1515

16-
Depending on your application dependencies, there could be three ways to provide the metadata with the Native Image Gradle Plugin:
16+
Depending on your application dependencies, there are three ways to provide the metadata with the Native Image Gradle Plugin:
1717

1818
1. [Using the Tracing Agent](#build-a-native-executable-with-the-agent)
1919
2. [Using the shared GraalVM Reachability Metadata Repository](#build-a-native-executable-using-the-graalvm-reachability-metadata-repository)

0 commit comments

Comments
 (0)