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 ?

3
  • Have you tried pb.environment().put("key", "value"); ? Commented Jan 18, 2013 at 12:43
  • @berry120 ya its kide of, but i did not get exact answer. Commented Jan 18, 2013 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) Commented Apr 1, 2019 at 14:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 updated my answer and added an example.
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));
    }

}

}

2 Comments

@masksim_khokhlov what if we again took the processBuilder instance and get the contained of map? it will not contain our variable
@BSalunke Why it won't? It will! ProcessBuilder.environment() will always return the same Map instance. Check out the javadoc.
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");

1 Comment

It looks like pb.start() is missing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.