<legend id='hFpV0'><style id='hFpV0'><dir id='hFpV0'><q id='hFpV0'></q></dir></style></legend>
    <bdo id='hFpV0'></bdo><ul id='hFpV0'></ul>

<small id='hFpV0'></small><noframes id='hFpV0'>

    1. <i id='hFpV0'><tr id='hFpV0'><dt id='hFpV0'><q id='hFpV0'><span id='hFpV0'><b id='hFpV0'><form id='hFpV0'><ins id='hFpV0'></ins><ul id='hFpV0'></ul><sub id='hFpV0'></sub></form><legend id='hFpV0'></legend><bdo id='hFpV0'><pre id='hFpV0'><center id='hFpV0'></center></pre></bdo></b><th id='hFpV0'></th></span></q></dt></tr></i><div id='hFpV0'><tfoot id='hFpV0'></tfoot><dl id='hFpV0'><fieldset id='hFpV0'></fieldset></dl></div>

      <tfoot id='hFpV0'></tfoot>
    2. System.getenv() 环境变量存在时返回 null

      System.getenv() returns null when the environment variable exists(System.getenv() 环境变量存在时返回 null)
    3. <small id='lM6TR'></small><noframes id='lM6TR'>

          • <bdo id='lM6TR'></bdo><ul id='lM6TR'></ul>
              <tfoot id='lM6TR'></tfoot>
              <i id='lM6TR'><tr id='lM6TR'><dt id='lM6TR'><q id='lM6TR'><span id='lM6TR'><b id='lM6TR'><form id='lM6TR'><ins id='lM6TR'></ins><ul id='lM6TR'></ul><sub id='lM6TR'></sub></form><legend id='lM6TR'></legend><bdo id='lM6TR'><pre id='lM6TR'><center id='lM6TR'></center></pre></bdo></b><th id='lM6TR'></th></span></q></dt></tr></i><div id='lM6TR'><tfoot id='lM6TR'></tfoot><dl id='lM6TR'><fieldset id='lM6TR'></fieldset></dl></div>
                <tbody id='lM6TR'></tbody>
                <legend id='lM6TR'><style id='lM6TR'><dir id='lM6TR'><q id='lM6TR'></q></dir></style></legend>
                本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                Java System Environment Variable(Java 系统环境变量)
                Java -classpath option(Java -classpath 选项)
                Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                Issue with JAVA_HOME(JAVA_HOME 的问题)
                  <bdo id='XY2ka'></bdo><ul id='XY2ka'></ul>

                    1. <i id='XY2ka'><tr id='XY2ka'><dt id='XY2ka'><q id='XY2ka'><span id='XY2ka'><b id='XY2ka'><form id='XY2ka'><ins id='XY2ka'></ins><ul id='XY2ka'></ul><sub id='XY2ka'></sub></form><legend id='XY2ka'></legend><bdo id='XY2ka'><pre id='XY2ka'><center id='XY2ka'></center></pre></bdo></b><th id='XY2ka'></th></span></q></dt></tr></i><div id='XY2ka'><tfoot id='XY2ka'></tfoot><dl id='XY2ka'><fieldset id='XY2ka'></fieldset></dl></div>
                    2. <tfoot id='XY2ka'></tfoot>
                    3. <small id='XY2ka'></small><noframes id='XY2ka'>

                        <legend id='XY2ka'><style id='XY2ka'><dir id='XY2ka'><q id='XY2ka'></q></dir></style></legend>
                            <tbody id='XY2ka'></tbody>