2

I'm defining an environment variable in a Linux shell with TestEnviron=varproperty. Now, I want to write a small program that reads the environment variable, writes it to the console output, and writes the variable to a properties file. However when I try it with this code getenv() returns null:

package javaenvironmentvariable;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

public class JavaEnvironmentVariable {
    public static void main(String[] args) {
        try {
            String variable = System.getenv("TestEnviron");
            System.out.println("TestEnviron: " + variable);
            variable = "TestEnviron=" + variable;

            Properties properties = new Properties();

            File file = new File("Variables.properties");
            FileOutputStream fileout = new FileOutputStream(file);
            properties.store(fileout, variable);
            fileout.close();
        } catch (Exception e) {
        }
    }
}

I use this in the shell to call the jar: sudo java -jar JavaEnvironmentVariable.jar

Tom
  • 16,842
  • 17
  • 45
  • 54
nick bijmoer
  • 99
  • 1
  • 2
  • 14
  • 1
    How did you define your variable in the shell ? Do you run your program right after defining it or in another shell ? How do you launch your program ? – perencia Sep 13 '15 at 09:54
  • If you use another shell to launch your program use `export` to define your var: `export TestEnviron=varproperty` – aghidini Sep 13 '15 at 09:54
  • @perencia I used TestEnviron=varproperty in the stell, and right after that i run my programm with this sudo java -jar JavaEnvironmentVariable.jar in the same shell. – nick bijmoer Sep 13 '15 at 09:57
  • @nickbijmoer you get your variable if you do `env` in your shell ? – perencia Sep 13 '15 at 10:01
  • 1
    Why use `sudo`? See [this question](http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo) for more info on `sudo` and env vars. You can launch your program with `sudo -E` to keep your environment in sudo. – aghidini Sep 13 '15 at 10:01
  • Reac what @AndreaGhidini told you. You missed the `export` part. – user207421 Sep 13 '15 at 10:05

3 Answers3

4

You should use sudo -E AND export to keep the environment variables:

$ export TestEnviron=varproperty
$ sudo -E java -jar JavaEnvironmentVariable.jar

However to be more safe you should configure sudo to keep your variable as explained in this post: How to keep Environment Variables when Using SUDO :

sudo visudo

and add this line:

Defaults env_keep += "TestEnviron"
Community
  • 1
  • 1
aghidini
  • 2,855
  • 5
  • 29
  • 32
2

Shells don't automatically put their variables into the environment. You need to explicitly export the variable using export ENVVAR=VALUE or set -x ENVVAR VALUE depending on your shell.

Functino
  • 1,939
  • 17
  • 25
0

I think your are setting the environment variable in one shell and executing your program in another shell. Try to execute both in a same shell. Before executing the program check by printing the environment variable in shell.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • I am executing them in the same shell but still get null, I printed the variable with this code: echo $TestEnviron and then it returns varproperty so thats fine. – nick bijmoer Sep 13 '15 at 10:03