- Latest
- 22.3
- 22.2
- 22.1
- 22.0
- 21.3
- Dev Build
- Native Image
- Build Output
- Build Configuration
- Tracing Agent
- Native Image Compatibility and Optimization Guide
- Class Initialization in Native Image
- Static Native Images
- Native Image Options
- Native Image Hosted and Runtime Options
- Native Image C API
- Implementing Native Methods in Java with Native Image
- LLVM Backend for Native Image
- Debug Info Feature
- Points-to Analysis Reports
- Using System Properties in Native Images
- Profile-Guided Optimizations
- Memory Management at Image Run Time
- Generating Heap Dumps from Native Images
- JDK Flight Recorder with Native Image
- JCA Security Services on Native Image
- Dynamic Proxy on Native Image
- Java Native Interface (JNI) on Native Image
- Reflection on Native Image
- Accessing Resources in Native Images
- Logging on Native Image
- URL Protocols on Native Image
- Native Image ARM64 Support
- GraalVM Updater
- Languages References
- Embedding Reference
- Polyglot Programming
Note
This documentation may be out of date. See the latest version.
Using System Properties in Native Images
Assume you have the following Java Program:
public class App {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}
If you compile that with, e.g., native-image -Dfoo=bar App
the system property foo
will be available at image build time.
For example, whenever you are in the code that is part of your application but executed at image build time (usually static field initializations and static initializers).
Thus if you execute the image above it will not contain foo
in the list of properties.
If, on the other hand, you execute the image with app -Dfoo=bar
, it will show foo
in the list of properties because you specified it for image run time.
In other words:
- Passing
-D<key>=<value>
tonative-image
affects properties seen at image build time. - Passing
-D<key>=<value>
to an image execution affects properties seen at image run time.
Access Environment Variables at Run Time #
Native image can also access environment variables at runtime. Consider the following example.
- Save this Java code into the EnvMap.java file:
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
var filter = args.length > 0 ? args[0] : "";
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
if(envName.contains(filter)) continue;
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
This code iterates over the environment variables and prints out the ones passing through the filter, passed as the command line argument.
- Compile and build a native image:
javac EnvMap.java
native-image EnvMap
- Run the resulting native image and pass some argument. It will correctly print out the values of the environment variables. For example:
./envmap HELLO
HELLOWORLD=hello world
export HELLOWORLD="world"
./envmap HELLO
HELLOWORLD=world