Java System.getenv() 在环境变量存在时返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29782467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.getenv() returns null when the environment variable exists
提问by SamTebbs33
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 文件和使用 .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
.
What could be the cause of this?
这可能是什么原因?
Edit:I followed the top answer here, restarted Eclipse and it worked!
编辑:我在这里遵循了最佳答案,重新启动了 Eclipse 并且它起作用了!
采纳答案by SomethingSomething
The answer to this question is more general than just System.getenv()
in Java.
这个问题的答案比System.getenv()
Java更普遍。
The environment variables only go 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 afteryou 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 中再次启动它,其中已经定义了环境变量。另一种选择是转到项目的运行配置并在那里定义环境变量。
P.S.
聚苯乙烯
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.
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.There are additional ways to define permanent environment variables. You can take a look herefor more information.
显然,如果您重新启动计算机,您在 shell 上定义的环境变量将不会被保存,因为您定义变量的 shell 进程将消失。
如果使用bash,则通过在文件中添加环境变量设置命令
~/.bashrc
,每次启动 bash 进程时都会执行该命令,可以模拟永久环境变量的行为。还有其他方法可以定义永久环境变量。您可以在此处查看更多信息。