Skip to content

Commit a57ec4b

Browse files
committed
[GR-37608] Provide a demo for Accessing Resources in Native Images doc
PullRequest: graal/11406
2 parents bc6d4b8 + c89424e commit a57ec4b

File tree

4 files changed

+993
-18
lines changed

4 files changed

+993
-18
lines changed

docs/getting-started/graalvm-enterprise/container-images/graalvm-ee-container-images.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ The following images are available:
1313

1414
| Image Name | Description
1515
------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|
16-
| [**jdk-ee**](https://container-registry.oracle.com/ords/ocr/ba/graalvm/jdk-ee) | A compact image containing the GraalVM Enterprise JDK. |
17-
| [**native-image-ee**](https://container-registry.oracle.com/ords/ocr/ba/graalvm/native-image-ee) | A compact image which includes the GraalVM Enterprise `native-image` utility and JDK |
18-
| [**enterprise**](https://container-registry.oracle.com/ords/ocr/ba/graalvm/enterprise) | Provides the GraalVM Enterprise JDK along with the `gu` (Graal Updater) utility to enable installation of additional features. |
19-
| [**nodejs-ee**](https://container-registry.oracle.com/ords/ocr/ba/graalvm/nodejs-ee) | Includes the Node.js runtime and the GraalVM Enterprise JDK. |
16+
| **jdk-ee** | A compact image containing the GraalVM Enterprise JDK. |
17+
| **native-image-ee** | A compact image which includes the GraalVM Enterprise `native-image` utility and JDK |
18+
| **enterprise** | Provides the GraalVM Enterprise JDK along with the `gu` (Graal Updater) utility to enable installation of additional features. |
19+
| **nodejs-ee** | Includes the Node.js runtime and the GraalVM Enterprise JDK. |
2020

2121
## Images Tagging Structure and Availability
2222

docs/reference-manual/embedding/embed-languages.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ permalink: /reference-manual/embed-languages/
1919
* [Embed languages in Guest Languages](#embed-languages-in-guest-languages)
2020
* [Build a Shell for Many Languages](#build-a-shell-for-many-languages)
2121
* [Step Through with Execution Listeners](#step-through-with-execution-listeners)
22-
* [Enterprise Sandbox Resource Limits](#enterprise-sandbox-resource-limits)
2322
* [Dependency setup](#dependency-setup)
2423

2524
The GraalVM Polyglot API lets you embed and run code from guest languages in JVM-based host applications.
@@ -492,7 +491,7 @@ public class Main {
492491
}
493492
```
494493

495-
In this code:
494+
In this code:
496495
- `import org.graalvm.polyglot.*` imports the base API for the Polyglot API.
497496
- `Engine.create()` creates a new engine instance with the default configuration.
498497
- `Source.create()` creates a source object for the expression “21 + 21”
@@ -532,7 +531,7 @@ public class Main {
532531
}
533532
```
534533

535-
In this code:
534+
In this code:
536535
- `Context.newBuilder().allowAllAccess(true).build()` builds a new outer context with all privileges.
537536
- `outer.eval` evaluates a JavaScript snippet in the outer context.
538537
- `inner = Java.type('org.graalvm.polyglot.Context').create()` the first JS script line looks up the Java host type Context and creates a new inner context instance with no privileges (default).
@@ -610,9 +609,6 @@ In this code:
610609
- The `context.eval()` call evaluates a specified snippet of guest language code.
611610
- The `listener.close()` closes a listener earlier, however execution listeners are automatically closed with the engine.
612611

613-
<!-- Enterprise Sandbox Resource Limits -->
614-
{% include_relative sandbox-options.md %}
615-
616612
## Polyglot Isolates
617613

618614
On GraalVM Enterprise, a Polyglot engine can be configured to run in a dedicated native image isolate.

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

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ permalink: /reference-manual/native-image/Resources/
66
---
77
# Accessing Resources in Native Images
88

9-
By default, the native image builder will not integrate any of the resources which are on the classpath during the generation into the final image.
10-
To make calls such as `Class.getResource()` or `Class.getResourceAsStream()` (or the corresponding ClassLoader methods) return specific resources (instead of null), the resources that should be accessible at image run time need to be explicitly specified. This can be done via a configuration file such as the following:
9+
By default, the `native-image` tool will not integrate any of the resources which are on the classpath during the generation into the final image.
10+
To make calls such as `Class.getResource()` or `Class.getResourceAsStream()` (or their corresponding `ClassLoader` methods) return specific resources (instead of `null`), you must specify the resources that should be accessible at run time.
11+
This can be achieved using a configuration file with the following content:
1112

1213
```json
1314
{
@@ -30,11 +31,11 @@ The configuration file's path must be provided to `native-image` with `-H:Resour
3031
Alternatively, individual resource paths can also be specified directly to `native-image`:
3132

3233
```shell
33-
native-image -H:IncludeResources=<Java regexp that matches resources to be included in the image> -H:ExcludeResources=<Java regexp that matches resources to be excluded from the image> ...
34+
native-image -H:IncludeResources="<Java regexp that matches resources to be included in the image>" -H:ExcludeResources="<Java regexp that matches resources to be excluded from the image>" ...
3435
```
3536
The `-H:IncludeResources` and `-H:ExcludeResources` options can be passed several times to define more than one regexp to match or exclude resources, respectively.
3637

37-
To see which resources get ultimately included into the image, you can enable the related logging info with `-H:Log=registerResource:`.
38+
To see which resources are included in the native executable, use the option `-H:Log=registerResource:<log level>`. The `<log level>` must be in the range from `1` to `5`, from least detailed to most detailed.
3839

3940
### Example Usage
4041

@@ -52,11 +53,73 @@ my-app-root
5253
```
5354
Then:
5455

55-
* All resources can be loaded with `.*/Resource.*txt$`, specified as `{"pattern":".*/Resource.*txt$"}` in a configuration file, or `-H:IncludeResources='.*/Resource.*txt$'` on the command line.
56+
* All resources can be loaded with `".*/Resource.*txt$"`, specified as `{"pattern":".*/Resource.*txt$"}` in a configuration file, or `-H:IncludeResources=".*/Resource.*txt$"` on the command line.
5657
* `Resource0.txt` can be loaded with `.*/Resource0.txt$`.
5758
* `Resource0.txt` and `Resource1.txt` can be loaded with `.*/Resource0.txt$` and `.*/Resource1.txt$`
5859
(or alternatively with a single `.*/(Resource0|Resource1).txt$`).
59-
* Also, if we want to include everything except the `Resource2.txt` file, we can simply exclude it with `-H:IncludeResources='.*/Resource.*txt$'` followed by `-H:ExcludeResources='.*/Resource2.txt$'`.
60+
* Also, if we want to include everything except the `Resource2.txt` file, we can simply exclude it with `-H:IncludeResources=".*/Resource.*txt$"` followed by `-H:ExcludeResources=".*/Resource2.txt$"`.
61+
62+
The following demo illustrates how to include a resource into a native executable. The application `fortune` simulates the traditional `fortune` Unix program (for more information, see [fortune](https://en.wikipedia.org/wiki/Fortune_(Unix)).
63+
64+
1. Save the following Java code into the _Fortune.java_ file:
65+
66+
```java
67+
import java.io.BufferedReader;
68+
import java.io.InputStreamReader;
69+
import java.util.ArrayList;
70+
import java.util.Random;
71+
import java.util.Scanner;
72+
73+
public class Fortune {
74+
75+
private static final String SEPARATOR = "%";
76+
private static final Random RANDOM = new Random();
77+
private ArrayList<String> fortunes = new ArrayList<>();
78+
79+
public Fortune(String path) {
80+
// Scan the file into the array of fortunes
81+
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(path))));
82+
s.useDelimiter(SEPARATOR);
83+
while (s.hasNext()) {
84+
fortunes.add(s.next());
85+
}
86+
}
87+
88+
private void printRandomFortune() throws InterruptedException {
89+
int r = RANDOM.nextInt(fortunes.size()); //Pick a random number
90+
String f = fortunes.get(r); //Use the random number to pick a random fortune
91+
for (char c: f.toCharArray()) { // Print out the fortune
92+
System.out.print(c);
93+
Thread.sleep(100);
94+
}
95+
}
96+
97+
public static void main(String[] args) throws InterruptedException {
98+
Fortune fortune = new Fortune("/fortunes.u8");
99+
fortune.printRandomFortune();
100+
}
101+
}
102+
```
103+
104+
2. Open the [_fortunes.u8_](assets/fortunes.u8) resource file and save it in the same directory as _Fortune.java_.
105+
106+
3. Compile:
107+
108+
```shell
109+
$JAVA_HOME/bin/javac Fortune.java
110+
```
111+
112+
4. Build a native executable by specifying the resource path:
113+
114+
```shell
115+
$JAVA_HOME/bin/native-image Fortune -H:IncludeResources=".*u8$"
116+
```
117+
118+
5. Run the executable image:
119+
120+
```shell
121+
./fortune
122+
```
60123

61124
See also the [guide on assisted configuration of Java resources and other dynamic features](BuildConfiguration.md#assisted-configuration-of-native-image-builds).
62125

@@ -94,7 +157,7 @@ Alternatively, bundles can be specified directly as options to `native-image` as
94157
native-image -H:IncludeResourceBundles=your.pgk.Bundle,another.pkg.Resource,etc.Bundle ...
95158
```
96159
By default, the requested bundles are included for all requested locales.
97-
In order to optimize this, it is possible to use ``IncludeResourceBundles`` with locale specific substring, for example ``-H:+IncludeResourceBundles=com.company.bundles.MyBundle_fr-FR`` will include the bundle only in French.
160+
In order to optimize this, it is possible to use `IncludeResourceBundles` with locale specific substring, for example `-H:+IncludeResourceBundles=com.company.bundles.MyBundle_fr-FR` will include the bundle only in French.
98161

99162
### Resources in Java modules
100163

@@ -124,6 +187,6 @@ will always work as expected for resources registered as described above (even i
124187
### JVM Mode of Localization
125188

126189
Resource Bundle lookup is a complex and dynamic mechanism which utilizes a lot of the infrastructure of JVM.
127-
As a result of that, it causes image size increase for smaller applications such as Hello World.
190+
As a result of that, it causes the size of the executable to increase for smaller applications such as `HelloWorld`.
128191
Therefore, an optimized mode is set by default in which this lookup is simplified utilizing the fact the all bundles are known ahead of time.
129192
In case you would like to use the original JVM lookup, use the `-H:-LocalizationOptimizedMode` option.

0 commit comments

Comments
 (0)