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

6
  • 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 ? Commented Sep 13, 2015 at 9:54
  • If you use another shell to launch your program use export to define your var: export TestEnviron=varproperty Commented Sep 13, 2015 at 9: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. Commented Sep 13, 2015 at 9:57
  • @nickbijmoer you get your variable if you do env in your shell ? Commented Sep 13, 2015 at 10:01
  • 1
    Why use sudo? See this question for more info on sudo and env vars. You can launch your program with sudo -E to keep your environment in sudo. Commented Sep 13, 2015 at 10:01

3 Answers 3

5

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah thats the solution! thanks a lot! I did not know that sudo wont keep the environment variables.
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.

1 Comment

It didn't work because I used sudo apparently but thanks for the export tip :)
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.