Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.getenv() returns null when the environment variable exists [duplicate]

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!

like image 914
SamTebbs33 Avatar asked Apr 21 '15 20:04

SamTebbs33


People also ask

Does System Getenv return null?

getenv() returns null when the environment variable exists - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Why would Getenv return null?

If the varname is not found, getenv() returns a NULL pointer. The returned value is NULL if the given variable is not currently defined.

What does System Getenv return?

getenv() returns a read-only Map. To obtain a single variable, call getenv with the variable name: String log_dir = System.

How does System Getenv work in Java?

getenv(String name) method gets the value of the specified environment variable. An environment variable is a system-dependent external named value. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).


1 Answers

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.

like image 140
SomethingSomething Avatar answered Sep 25 '22 05:09

SomethingSomething