System.getenv()不会列出所有环境变量 [英] System.getenv() does not list all the environment variables

查看:796
本文介绍了System.getenv()不会列出所有环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的某些环境变量没有被JVM吸收.

I have noticed that some of my environment variables are not being picked up by the JVM.

在我的.bash_profile中,我定义了以下内容:

In my .bash_profile I defined the following:

IO_HOME='some_value'
export IO_HOME

并通过在shell中进行操作:

and by doing in shell:

echo $IO_HOME

我得到正确的结果.

但是System.getProperties()System.getenv()都没有显示此变量已设置.我尝试了Java 6和Java 7.

But neither System.getProperties() nor System.getenv() is showing this variable being set. I tried both Java 6 and Java 7.

有什么我想念的吗?

推荐答案

将环境导出到生成的进程非常稳定;如果System.getenv()不包含变量,则是因为它不在环境中.有几件要检查的事情,都与该过程的启动方式有关:

Exporting environment to spawned processes is pretty stable; if System.getenv() is not including a variable then it is because it is not in the environment. A couple of things to check, both relating to how the process is started:

  • 您是否从导出变量的环境中启动Java进程?例如,如果它在您的.bash_profile中,并且您正在从菜单或桌面执行Java程序,则必须将其添加到.bash_profile中后注销并登录,以便桌面看到该变量.

  • Are you starting the java process from an environment where the variable is exported? For example, if it is in your .bash_profile and you are executing the java program from a menu or desktop then you have to log out and log in after adding it in .bash_profile for your desktop to see the variable.

是否已将该变量从环境中显式删除?与大多数情况一样, ProcessBuilder 允许此操作产生流程的API.

Is the variable explicitly removed from environment for the process? ProcessBuilder allows this, as do most of all APIs that spawn processes.

要尝试的一件事是在确保变量已在该Shell中导出之后,从命令行Shell启动该过程.

One thing to try is to start the process from command line shell, after ensuring the variable is exported in that shell.

这篇关于System.getenv()不会列出所有环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆