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