Possible Duplicate:
How do I set environment variables from Java?
I'm working on Java. I have to add an environment variable in java code programmatic such that it will be available when i get list using process builder as follows:
import java.util.Map;
import java.util.Set;
class helloworld {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("export MY_ENV_VAR=1");
Map<String, String> envMap = pb.environment();
Set<String> keys = envMap.keySet();
for(String key:keys){
System.out.println(key+" ==> "+envMap.get(key));
}
}
}
But with above trial i cant get environment variable properly. so How to set the environment variable ?
You can add the desired variables directly into ProcessBuilder.environment()
map.
The code below should work:
import java.util.Map;
import java.util.Set;
class helloworld {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("/bin/sh"); // or any other program you want to run
Map<String, String> envMap = pb.environment();
envMap.put("MY_ENV_VAR", "1");
Set<String> keys = envMap.keySet();
for(String key:keys){
System.out.println(key+" ==> "+envMap.get(key));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With