java 环境变量的修改,如何在Java程序中检索环境变量(外部修改的)的修改值?...

Is it possible to retrieve the modified value of an environment variable in a Java program?

I tried using System.getenv(). But the new value is not reflected in the program. The scenario is this:

The program retrieves the value of an environment variable.

The value of this variable is changed externally, while the program is still running. (It could be even manual process, like for eg, in Windows, the user changes the value from Advanced tab in My Computer->properties)

The program retrieves the value of the environment variable again.

The second time the value is retrieved, it should get the modified value. I found that using System.getenv() gave me the old value only. Is there any reliable method for this? (This requirement is specific to Windows, but would love to hear general solutions too)

解决方案

Afaik, environment variables visible to a process are set up at process creation time.

Subsequent modifications to the OS variables are not visible to process other than that who change them.

However, depending on the OS, there could be some way to retrieve them. E.g. on Windows Os, they are kept in the registry, so you can read from them.

Try reading the key at HKCU\Environment for current user variables, and the key

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment for system variables.

See this question about Windows registry for a useful class to read the registry.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值