Map<String, String> env = pb.environment(); env.put("MV_ENV_VAR", "1");
set MY_ENV_VAR = 1. Before you invoke the process
Process p = pb.start();
export interpreted only by the shell.
See also ProcessBuilder
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 a managed environment.
stacker
source share