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 ?
would set MY_ENV_VAR=1. Before you invoke the Process by
exportwould only be interpreted by a shell.See also ProcessBuilder
A full example:
prints among other environment values:
This should prove that the created process uses the manipulated environment.