12

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 ?

Community
  • 1
  • 1
BSalunke
  • 11,499
  • 8
  • 34
  • 68
  • Have you tried `pb.environment().put("key", "value");` ? – Peter Lawrey Jan 18 '13 at 12:43
  • @berry120 ya its kide of, but i did not get exact answer. – BSalunke Jan 18 '13 at 12:49
  • ```java java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableMap.put(Collections.java:1457) at br.com.stilingue.services.remove_occupation.RemoveOccupationTest.setUpTestEnv(RemoveOccupationTest.java:29) ``` – Michael Pacheco Apr 01 '19 at 14:45

3 Answers3

9
 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.

stacker
  • 68,052
  • 28
  • 140
  • 210
  • thanks, but env.put("MV_ENV_VAR", "1"); will set the env variable only in local variable, but if u again take processbuilder instance and get the content of map, it will not show the variable that we have set as mentioned by you. – BSalunke Jan 18 '13 at 12:46
  • @BSalunke updated my answer and added an example. – stacker Jan 18 '13 at 13:04
5

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));
    }

}

}

maksim_khokhlov
  • 794
  • 5
  • 8
  • @masksim_khokhlov what if we again took the processBuilder instance and get the contained of map? it will not contain our variable – BSalunke Jan 18 '13 at 12:52
  • 1
    @BSalunke Why it won't? It will! ProcessBuilder.environment() will always return the same `Map` instance. Check out the [javadoc](http://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html). – maksim_khokhlov Jan 18 '13 at 13:14
3

You can get the environment variable with the process Builder object :

    ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
    Map<String, String> env = pb.environment();
    env.put("VAR1", "myValue");
    env.remove("OTHERVAR");
    env.put("VAR2", env.get("VAR1") + "suffix");
Dimitri
  • 8,122
  • 19
  • 71
  • 128