4

I'm trying to set an environmental variable in linux. I followed the instructions here: Make $JAVA_HOME easily changable in Ubuntu

Despite using source /etc/environment and using echo MY_VAR to verify that linux detects the variable, my java app will not pick up on it. The variable continues to return null

public static void main(String[] args) throws IOException {
    System.out.print(System.getenv("MY_VAR"));

I'm executing my java application via sudo java -jar /path/to/my.jar

Update: My mistake, I hadn't included the correct command. I'm actually sudoing.

Community
  • 1
  • 1
Ben
  • 60,438
  • 111
  • 314
  • 488
  • 1
    Are u able to get other variables such as JAVA_HOME? – Juned Ahsan Oct 09 '13 at 11:47
  • try setting JAVA_HOME it in .bashrc in /home – codeMan Oct 09 '13 at 11:48
  • 7
    Try printing out System.getenv(). This will return a map. Display that to see which all variables u are able to see. Then check if the key is same as the one that you specified while setting the variable – Pratik Shelar Oct 09 '13 at 11:51
  • Please copy your shell session into the question, so that we can see what exactly you're doing. – NPE Oct 09 '13 at 12:01
  • Are you sure that the variables are the same? Your question suggests that one is `MY_VAR` and the other is `MY_ENVIRONMENT`. – devnull Oct 09 '13 at 12:05
  • @devnull - Yes sorry, that was a mistake in my example, but the actual vars are the same. – Ben Oct 09 '13 at 12:32
  • @Pratik - Done, and my environment variable doesn't show up there at all – Ben Oct 09 '13 at 12:33
  • @Webnet if you run the Java program as a different user then have a look here http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo how to pass the environment variable – SubOptimal Oct 09 '13 at 14:51
  • 2
    Did you try the -E flag to sudo? http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo – FuriousGeorge Oct 09 '13 at 14:54

3 Answers3

3

You need to export the variable

export MY_VAR=stackoverflow
java -jar /path/to/my.jar

then you can print the value with

System.out.print(System.getenv("MY_VAR"));

edit: short example script (amend the path if necessary)

#!/bin/sh

MY_VAR="foobaz not exported"
echo "MY_VAR: ${MY_VAR}"
java -jar my.jar

export MY_VAR="foobaz exported"
echo "MY_VAR: ${MY_VAR}"
java -jar my.jar
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
1

from the linked answer ..

Execute "source /etc/environment" in every shell where you want the variables to be updated:

When you call java -jar /path/to/my.jar I think you will be starting a new shell, meaning that the contents of ./etc/environment wont be available to the shell your java code is running in.

try

export MY_ENVIRONMENT="HELLO"
java -jar /path/to/my.jar

Does that look any better?

And if you are sudo -ing your command ...

sudo -c export MY_ENVIRONMENT="HELLO";java -jar /path/to/my.jar

or something along those lines

DaveH
  • 7,187
  • 5
  • 32
  • 53
1

The issue is that sudo runs with it's own set of environment variables. You can either add the variable to the root environment (sometimes finicky), or tell sudo to maintain the current environment's variables explicitly.

Here's how you would do the latter:

  1. Open up your bash profile vim ~/.bash_profile
  2. Add the environment variable to the file export MY_VAR=80
  3. Open up the sudoers config file sudo visudo
  4. Add the following line to the file exactly as so Defaults env_keep +="MY_VAR"

Now when you run sudo java -jar /path/to/my.jar it should work as desired, with MY_VAR set to 80.

Kyle Chadha
  • 3,741
  • 2
  • 33
  • 42