如何在Java程序中检索环境变量(外部修改的)的修改值? [英] How to retrieve the modified value of an environment variable (which is modified externally) in a Java program?

查看:160
本文介绍了如何在Java程序中检索环境变量(外部修改的)的修改值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在Java程序中检索环境变量的修改值吗?

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

我尝试使用 System.getenv()。但是新的价值没有体现在这个计划中。情况是这样的:

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


  1. 程序检索环境变量的值。

  1. The program retrieves the value of an environment variable.

当程序仍在运行时,此变量的值将从外部更改。 (它甚至可以是手动过程,例如,在Windows中,用户从我的电脑→属性中的高级选项卡更改值)

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.

第二次检索该值时,应该获得修改后的值。我发现使用 System.getenv()仅给我旧值。有什么可靠的方法吗? (这个要求是特定于 Windows ,但也希望听到一般解决方案)

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 ,过程创建时设置一个进程可见的环境变量。

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.

但是,取决于操作系统,可能有一些方法来检索它们。例如。在Windows Os上,它们保存在注册表中,以便您可以从中读取。

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.

尝试阅读 HKCU\Environment 用于当前用户变量,并且键值

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

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

请参阅关于Windows注册表的这个问题,一个有用的类读取注册表。

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

这篇关于如何在Java程序中检索环境变量(外部修改的)的修改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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