简体   繁体   English

System.getenv()未列出所有环境变量

[英]System.getenv() does not list all the environment variables

I have noticed that some of my environment variables are not being picked up by the JVM. 我注意到我的一些环境变量没有被JVM接收。

In my .bash_profile I defined the following: 在我的.bash_profile我定义了以下内容:

IO_HOME='some_value'
export IO_HOME

and by doing in shell: 并通过在shell中做:

echo $IO_HOME

I get the correct result. 我得到了正确的结果。

But neither System.getProperties() nor System.getenv() is showing this variable being set. 但是System.getProperties()System.getenv()都没有显示正在设置的变量。 I tried both Java 6 and Java 7. 我尝试了Java 6和Java 7。

Is there something I am missing? 有什么我想念的吗?

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. 如果System.getenv()不包含变量,那么它是因为它不在环境中。 A couple of things to check, both relating to how the process is started: 要检查的几件事情,都与流程的启动方式有关:

  • Are you starting the java process from an environment where the variable is exported? 您是从导出变量的环境启动Java进程吗? 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. 例如,如果它位于.bash_profile中,并且您正在从菜单或桌面执行java程序,那么在将其添加到.bash_profile中后,您必须注销并登录,以便桌面查看变量。

  • Is the variable explicitly removed from environment for the process? 变量是否明确地从过程的环境中删除了? ProcessBuilder allows this, as do most of all APIs that spawn processes. ProcessBuilder允许这样做,大多数产生进程的API也是如此。

One thing to try is to start the process from command line shell, after ensuring the variable is exported in that shell. 在确保在该shell中导出变量之后,要尝试的一件事是从命令行shell启动该过程。

From Windows, I recently saw some crazy behaviour where IntelliJ refused to show all env vars from System.getenv() after setting either user or system env vars. 在Windows中,我最近看到一些疯狂的行为,其中IntelliJ在设置用户或系统环境变量后拒绝显示来自System.getenv()所有env变量。 The trick was to launch IntelliJ from a DOS box. 诀窍是从DOS框启动IntelliJ。 For Windows users: Maybe a reboot (or logoff/logon) will fix the issue. 对于Windows用户:可能重启(或注销/登录)将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM