A native executable accesses your environment variables in the same way as a regular Java application. For example, assume you have the following source code:
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)) {
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
}
This code iterates over your environment variables and prints out the ones that contain the String of characters passed as the command-line argument.
Make sure you have installed a GraalVM JDK. The easiest way to get started is with SDKMAN!. For other installation options, visit the Downloads section.
javac EnvMap.java
native-image EnvMap
./envmap HELLO
<no output>
bash
shell, follow the example below.) Now, run the native executable again–it will correctly print out the name and value of the matching environment variable(s).
export HELLOWORLD='Hello World!'
./envmap HELLO
You should receive the expected output:
HELLOWORLD=Hello World!