This question already has an answer here:
这个问题在这里已有答案:
- Environment variables in Mac OS X 8 answers
Mac OS X 8环境变量的答案
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()
.
我正在尝试System.getenv()来获取我通过终端(Mac)设置的环境变量的值,我还在我的.bash_profile文件中设置变量并重新加载。在这样做之后,我回显了值并且正确的值被打印到终端。尝试检索变量的值时(我确保在我的.bash_profile文件和使用System.getenv()时使用了正确的名称。
In the below code, I have replaced the name of the variable with VAR_NAME:
在下面的代码中,我用VAR_NAME替换了变量的名称:
String varValue = System.getenv("VAR_NAME");
System.out.println("Value: " + varValue);
In my .bash_profile:
在我的.bash_profile中:
export VAR_NAME="foo"
"null" is printed when I print out the value of varValue
.
当我打印出varValue的值时,会打印“null”。
What could be the cause of this?
可能是什么原因造成的?
Edit: I followed the top answer here, restarted Eclipse and it worked!
编辑:我在这里按照最新的答案,重新启动Eclipse,它工作正常!
1 个解决方案
#1
53
The answer to this question is more general than just System.getenv()
in Java.
这个问题的答案比Java中的System.getenv()更通用。
The 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 had been defined on your shell when you launched Eclipse.
环境变量仅在进程树下进行,并且仅在启动进程时才进行。 Eclipse是shell的子进程 - 因此,它继承了在启动Eclipse时在shell上定义的所有环境变量。
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.
您可能在启动Eclipse后在shell上定义了环境变量。因此,Eclipse和它创建的子Java流程永远不会“知道”您的新环境变量。
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.
由于这种行为,实际上这里的解决方案是退出Eclipse并再次从shell启动它,其中已经定义了环境变量。另一种选择是转到项目的运行配置并在那里定义环境变量。
#1
53
The answer to this question is more general than just System.getenv()
in Java.
这个问题的答案比Java中的System.getenv()更通用。
The 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 had been defined on your shell when you launched Eclipse.
环境变量仅在进程树下进行,并且仅在启动进程时才进行。 Eclipse是shell的子进程 - 因此,它继承了在启动Eclipse时在shell上定义的所有环境变量。
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.
您可能在启动Eclipse后在shell上定义了环境变量。因此,Eclipse和它创建的子Java流程永远不会“知道”您的新环境变量。
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.
由于这种行为,实际上这里的解决方案是退出Eclipse并再次从shell启动它,其中已经定义了环境变量。另一种选择是转到项目的运行配置并在那里定义环境变量。