Skip to content

Commit 02621f5

Browse files
committed
Take out Use System Properties in a Native Executable tab on Guides page
1 parent 4e5a87c commit 02621f5

File tree

1 file changed

+2
-107
lines changed

1 file changed

+2
-107
lines changed

docs/reference-manual/native-image/guides/use-system-properties.md

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3,114 +3,9 @@ layout: ni-docs
33
toc_group: how-to-guides
44
link_title: Use System Properties
55
permalink: /reference-manual/native-image/guides/use-system-properties/
6-
redirect_to: /reference-manual/native-image/overview/Options/
6+
redirect_to: https://github.com/graalvm/graalvm-demos/tree/master/native-image/use-system-properties
77
---
88

99
# Use System Properties in a Native Executable
1010

11-
Assume you have compiled the following Java application using `javac`:
12-
```java
13-
public class App {
14-
public static void main(String[] args) {
15-
System.getProperties().list(System.out);
16-
}
17-
}
18-
```
19-
20-
If you build a native executable using `native-image -Dfoo=bar App`, the system property `foo` will **only** be available at build time.
21-
This means it is available to the [code in your application that is run at build time](http://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/ImageInfo.html#inImageBuildtimeCode--) (usually static field initializations and static initializers).
22-
But if you run the resulting executable, it will not contain `foo` in the printed list of properties.
23-
24-
If, on the other hand, you run the executable with `app -Dfoo=bar`, it will display `foo` in the list of properties because you specified this property.
25-
26-
## Read System Properties at Build Time
27-
28-
You can read system properties at build time and incorporate them into the native executable, as shown in the following example.
29-
30-
### Prerequisite
31-
Make sure you have installed a GraalVM JDK.
32-
The easiest way to get started is with [SDKMAN!](https://sdkman.io/jdks#graal).
33-
For other installation options, visit the [Downloads section](https://www.graalvm.org/downloads/).
34-
35-
1. Save the following Java code into a file named _ReadProperties.java_:
36-
```java
37-
public class ReadProperties {
38-
private static final String STATIC_PROPERTY_KEY = "static_key";
39-
private static final String INSTANCE_PROPERTY_KEY = "instance_key";
40-
private static final String STATIC_PROPERTY;
41-
private final String instanceProperty;
42-
static {
43-
System.out.println("Getting value of static property with key: " + STATIC_PROPERTY_KEY);
44-
STATIC_PROPERTY = System.getProperty(STATIC_PROPERTY_KEY);
45-
}
46-
47-
public ReadProperties() {
48-
System.out.println("Getting value of instance property with key: " + INSTANCE_PROPERTY_KEY);
49-
instanceProperty = System.getProperty(INSTANCE_PROPERTY_KEY);
50-
}
51-
52-
public void print() {
53-
System.out.println("Value of instance property: " + instanceProperty);
54-
}
55-
56-
public static void main(String[] args) {
57-
System.out.println("Value of static property: " + STATIC_PROPERTY);
58-
ReadProperties rp = new ReadProperties();
59-
rp.print();
60-
}
61-
}
62-
```
63-
64-
2. Compile the application:
65-
```shell
66-
javac ReadProperties.java
67-
```
68-
69-
3. Build the native executable, passing a system property as a command-line option. Then run the native executable, passing a different system property on the command line.
70-
```shell
71-
native-image -Dstatic_key=STATIC_VALUE ReadProperties
72-
```
73-
```shell
74-
./readproperties -Dinstance_key=INSTANCE_VALUE
75-
```
76-
77-
You should see the following output:
78-
```shell
79-
Getting value of static property with key: static_key
80-
Value of static property: null
81-
Getting value of instance property with key: instance_key
82-
Value of instance property: INSTANCE_VALUE
83-
```
84-
85-
This indicates that the class static initializer was not run at build time, but at **run time**.
86-
87-
4. To force the class static initializer to run at build time, use the `--initialize-at-build-time` option, as follows:
88-
```shell
89-
native-image --initialize-at-build-time=ReadProperties -Dstatic_key=STATIC_VALUE ReadProperties
90-
```
91-
In the output from the `native-image` tool you should see the message like this:
92-
```
93-
GraalVM Native Image: Generating 'readproperties' (executable)...
94-
==========================================================================
95-
Getting value of static property with key: static_key
96-
[1/8] Initializing... (4.0s @ 0.13GB)
97-
...
98-
```
99-
100-
5. Run the executable again, as follows:
101-
```shell
102-
./readproperties -Dinstance_key=INSTANCE_VALUE
103-
```
104-
105-
This time you should see the following output, confirming that the static initializer was run at **build time**, not at run time.
106-
107-
```shell
108-
Value of static property: STATIC_VALUE
109-
Getting value for instance property key: instance_key
110-
Value of instance property: INSTANCE_VALUE
111-
```
112-
113-
### Related Documentation
114-
115-
* [Command-line Options: System Properties](../BuildOptions.md#system-properties)
116-
* [Specify Class Initialization Explicitly](specify-class-initialization.md)
11+
This guide has been moved to the [graalvm-demos](https://github.com/graalvm/graalvm-demos/tree/master/native-image/use-system-properties) repository.

0 commit comments

Comments
 (0)