Map<String, String> env = pb.environment();
env.put("MV_ENV_VAR", "1");
would set MY_ENV_VAR=1. Before you invoke the Process by
Process p = pb.start();
export
would only be interpreted by a shell.
See also ProcessBuilder
A full example:
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
env.put("MYVAR", "myValue");
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
char[] buf = new char[1024];
while (!isr.ready()) {
;
}
while (isr.read(buf) != -1) {
System.out.println(buf);
}
}
prints among other environment values:
MYVAR=myValue
This should prove that the created process uses the manipulated environment.