System.getenv() 环境变量存在时返回 null

System.getenv() returns null when the environment variable exists(System.getenv() 环境变量存在时返回 null)

本文介绍了System.getenv() 环境变量存在时返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 System.getenv() 来获取我通过终端 (Mac) 设置的环境变量的值,我还在我的 .bash_profile 文件中设置了变量并重新加载.这样做之后,我回显了该值,并将正确的值打印到终端.尝试检索变量的值时(我确保在 .bash_profile 文件和使用 System.getenv() 时都使用了正确的名称.

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().

在下面的代码中,我用 VAR_NAME 替换了变量的名称:

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);

在我的 .bash_profile 中:

In my .bash_profile:

export VAR_NAME="foo"

当我打印出 varValue 的值时,会打印出null".

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

这可能是什么原因?

我遵循了最佳答案 这里,重启 Eclipse 就可以了!

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

推荐答案

这个问题的答案比Java中的System.getenv()更笼统.

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

环境变量仅在进程树下,并且仅在启动进程时.Eclipse 是你的 shell 的一个子进程(它也是一个进程)——因此,它继承了 在你启动 Eclipse 时在你的 shell 上定义的所有环境变量.

The environment variables only go down the process tree and only when a process is launched. Eclipse is a child process of your shell (which is also a process) - hence, it inherited all the environment variables that had been defined on your shell when you launched Eclipse.

您可能在启动 Eclipse 之后在 shell 上定义了环境变量.因此,Eclipse 和它创建的子 Java 进程永远不会知道".关于你的新环境变量.

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 中再次启动它,其中环境变量 已定义.另一种选择是转到项目的运行配置并在那里定义环境变量.

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.

附言

  1. 很明显,如果你重启你的电脑,你在shell中定义的环境变量将不会被保存,因为你定义变量的shell进程将会消失.

  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.

如果你使用bash,那么通过在~/.bashrc文件中添加环境变量设置命令,每次bash进程执行时都会执行该命令启动后,您可以模拟永久环境变量的行为.

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.

还有其他方法可以定义永久环境变量.您可以查看此处了解更多信息.p>

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

这篇关于System.getenv() 环境变量存在时返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:System.getenv() 环境变量存在时返回 null

基础教程推荐