75

I am trying to System.getenv() to get the value of an environment variable that I have set through my terminal (Mac), I also set the variable in my .bash_profile file and reloaded. After doing so, I echo'd the value and the correct value was printed to the terminal. When trying to retrieve the value of the variable (I made sure I was using the correct name in both my .bash_profile file and when using System.getenv().

In the below code, I have replaced the name of the variable with VAR_NAME:

String varValue = System.getenv("VAR_NAME");
System.out.println("Value: " + varValue);

In my .bash_profile:

export VAR_NAME="foo"

"null" is printed when I print out the value of varValue.

What could be the cause of this?

Edit: I followed the top answer here, restarted Eclipse and it worked!

Community
  • 1
  • 1
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
  • 2
    How are you running the application? Does `printenv | grep VAR_NAME` in your shell show the value? – Etan Reisner Apr 21 '15 at 20:28
  • It does, and I'm running it through Eclipse @EtanReisner – SamTebbs33 Apr 21 '15 at 20:33
  • 1
    You are running your shell command in Eclipse? – RealSkeptic Apr 21 '15 at 20:35
  • 1
    Can you check if http://stackoverflow.com/questions/14562156/how-to-set-java-environment-variable-on-mac or http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x?lq=1 answer your question ? – JFPicard Apr 21 '15 at 20:35
  • @RealSkeptic Nope, I'm running the application via Eclipse – SamTebbs33 Apr 21 '15 at 20:37
  • 2
    So the answers recommended by JFPicard should help you. The issue is to understand that an environment variable only affects what's in the same process where it was set or its child processes. And .profile only affects terminal sessions. – RealSkeptic Apr 21 '15 at 20:39
  • @JFPicard Thanks! I followed the second link you gave me and it worked. If you post it as an answer here, I'll mark it as correct so others know I have an accepted answer. – SamTebbs33 Apr 21 '15 at 20:44
  • The answer to this should be more general in my opinion - one should know that environment variables go only down the process tree and only when a process is launched. Eclipse is a child process of your shell - hence, it inherited all the environment variables that were defined on your shell **when you launched it**. – SomethingSomething Apr 21 '15 at 22:12

1 Answers1

130

The answer to this question is more general than just System.getenv() in Java.

Every process has its own independent copy of environment variables, while the environment variables only go down the process tree and are copied from parent to child only when the child process is created. In your case, your shell, which itself is a process, started/created the Eclipse process. Therefore, Eclipse is a child process of your shell and therefore, the environment variables defined on your Eclipse instance are a copy of those that had been defined on your shell when you launched Eclipse.

You probably defined the environment variable on your shell after you had launched Eclipse. Hence, Eclipse and the child Java processes it created, would never "know" about your new environment variable.

Due to this behavior, actually the solution here is to exit Eclipse and launch it again from your shell, in which the environment variable is already defined. Another option is to go to the run configuration of the project and define there the environment variable.

P.S.

  1. Obviously, if you restart your computer, the environment variables you have defined on your shell will not be saved, simply since the shell process you defined the variables on will be gone.

  2. If you use bash, then by adding the environment variable setting command to the file ~/.bashrc, which is executed each time a bash process is started, you can simulate the behavior of permanent environment variables.

  3. There are additional ways to define permanent environment variables. You can take a look here for more information.

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 8
    The environment variable was in fact defined before launching Eclipse, I even restarted my computer after defining the variable back when it wasn't working as intended. The answer given in the link I gave in my OP fixed the issue. – SamTebbs33 Apr 21 '15 at 22:27
  • It of course didn't work after you restarted the computer, as these variables are defined per process. They don't stay defined forever... – SomethingSomething Apr 22 '15 at 09:13
  • You can imagine the environment variables mechanism as a hash-table that exists along a process. You can always update it by adding/removing/changing variables. When you close the process (e.g. by exiting the shell or by restarting your computer), this hash table dies with the process. Another important thing I've already written in the answer is that every process inherits (actually copies) this hash table from the process which created it, at the creation time. After the creation, they have different hash tables, none of them knows about the other – SomethingSomething Apr 22 '15 at 09:17
  • If you understand this theory, you will understand why the solution in your link worked for you – SomethingSomething Apr 22 '15 at 09:19
  • It worked when I compiled file from console. Means it was not available in eclipse. Thanks for help you saved my time. – Parveen Verma Dec 27 '16 at 09:32
  • I don't run Eclipse from the shell and the environment variable is still null. Why do I have to run Eclipse for this to work ? Shouldn't this environment variable be accessible anywhere like `PATH` ? – Islam Azab Dec 28 '16 at 14:21
  • 2
    Just an observation: if you're Windows user, restarting Eclipse won't work for this approach, you need to explicitly **Close** and **Open** the IDE again to get the variables applied. – diogo Jan 21 '17 at 14:00
  • In Windows, in addition to "local" environment variables, which are described in the answer, there are global "permanent" environment variables. If you define a "local" variable on the shell and then open Eclipse not from that specific shell, this variable will of course not be passed to Eclipse. Only in Windows, using permanent environment variables, one can open Eclipse by clicking on its Icon and have it with the environment variables set (not only for Eclipse, but for any launched application) – SomethingSomething May 21 '17 at 07:58
  • How can I made a new variable and using it forever and from any where (not in the same shell) ? – Nguyễn Thắng Jan 07 '18 at 08:14
  • @NguyễnThắng If you work in linux, you may need to edit `~/.bashrc` (given that you're using `bash` as a shell, else, you'll need to edit a file with similar name that is related to your shell). All the commands in `~/.bashrc` are executed every time `bash` shell is starting. So adding the `export` command in this file should do the work. In windows, take a look at this: http://www.forbeslindesay.co.uk/post/42833119552/permanently-set-environment-variables-on-windows – SomethingSomething Jan 07 '18 at 11:10
  • I added to /etc/environment. It's work :D – Nguyễn Thắng Jan 08 '18 at 08:49
  • Once i define the variable within my `.bash_profile`, now System.getEnv works. Thanks. I think using System.getProperty is better because it gives more flexibility. – djangofan Dec 09 '20 at 17:54