如何在Windows上持久定义Java中的环境变量?

时间:2022-08-04 23:25:45

Is there a way to change Windows environment variables using Java? I tried with the cmd function set:

有没有办法使用Java更改Windows环境变量?我尝试使用cmd函数集:

Process exec = Runtime.getRuntime().exec(new String[] {
      "cmd", "/c", "set", "HTTP_PROXY=" + PROXY_URL
});

if (exec.waitFor() != 0) {
    throw new IllegalStateException("Output: "
        + getText(exec.getInputStream())
        + "Error: " + getText(exec.getErrorStream()
        + "\n"
        + "Exit value: " + exec.exitValue());
}

This code runs fine without any error but when I later check system variables nothing has changed.

这段代码运行正常,没有任何错误但是当我稍后检查系统变量时没有任何改变。

I'm trying to update HTTP_PROXY so that other software run behind an HTTP proxy can use it.

我正在尝试更新HTTP_PROXY,以便在HTTP代理后面运行的其他软件可以使用它。

2 个解决方案

#1


Processes are launched in an enviroment made of name-value pairs. When a program writes to an env variable, it can optionally make that write visible to child processes, but when you check the value you are likely using another process (maybe run via cmd.exe) that has no access to the environment of your Java program.

流程在由名称 - 值对组成的环境中启动。当程序写入env变量时,它可以选择使该写入对子进程可见,但是当您检查该值时,您可能正在使用另一个无法访问Java环境的进程(可能通过cmd.exe运行)程序。

I don't know if the purpose of your code is to define environment variables in a persistent manner, but if that's the case it can't be done in Java without specific OS tools and in a platform-independent way.

我不知道你的代码的目的是以持久的方式定义环境变量,但如果是这种情况,如果没有特定的OS工具并且与平台无关的方式,它就无法在Java中完成。

Environment variables default values must be stored by system tools in places where they are then read by the very same system tools from. On Linux there are files like .profile and .bashrc in the user home, while on Windows you have the registry. For example on my Windows 7 I have: my PATH default value stored in \HKEY_CURRENT_USER\Environment - so you must find a way to write to the system registry, for example the command reg

环境变量默认值必须由系统工具存储在可以通过相同系统工具读取的位置。在Linux上,用户家中有.profile和.bashrc等文件,而在Windows上则有注册表。例如,在我的Windows 7上,我有:我的PATH默认值存储在\ HKEY_CURRENT_USER \ Environment中 - 所以你必须找到一种写入系统注册表的方法,例如命令reg

The REG ADD command allows the user to add new keys and values to the Registry. To display the full range of parameters that can be used, type the following into the command line: reg add /?

REG ADD命令允许用户向注册表添加新的键和值。要显示可以使用的所有参数,请在命令行中键入以下内容:reg add /?

To add the key HKLM\Software\MyNewApp on remote computer PC2, type:

要在远程计算机PC2上添加密钥HKLM \ Software \ MyNewApp,请键入:

REG ADD \\PC2\HKLM\Software\MyNewApp

To add a registry entry to HKLM\Software\MyNewApp with a value named Data of type REG_BINARY and data of fe340ead, type:

要使用名为Data类型为REG_BINARY的数据和fe340ead数据的值向HKLM \ Software \ MyNewApp添加注册表项,请键入:

REG ADD HKLM\Software\MyNewApp /v Data /t REG_BINARY /d fe340ead

You can either write a .bat script or call reg from your Java program. You may need administrator priviledges and to restart some programs (eventually the whole machine) for the update to take effect (for example restart explorer.exe)

您可以编写.bat脚本或从Java程序中调用reg。您可能需要管理员权限并重新启动某些程序(最终是整个计算机)才能使更新生效(例如,重新启动explorer.exe)

#2


It's impossible to do that (at least for forever). There are good reasons for that (isolation, java tools unknowingly changing your env). Look for a hack here :

这样做是不可能的(至少是永远的)。这有很好的理由(隔离,java工具在不知不觉中改变你的环境)。在这里找一个黑客:

edit:

a complex explanation was given by raffaele

拉斐尔给出了一个复杂的解释

#1


Processes are launched in an enviroment made of name-value pairs. When a program writes to an env variable, it can optionally make that write visible to child processes, but when you check the value you are likely using another process (maybe run via cmd.exe) that has no access to the environment of your Java program.

流程在由名称 - 值对组成的环境中启动。当程序写入env变量时,它可以选择使该写入对子进程可见,但是当您检查该值时,您可能正在使用另一个无法访问Java环境的进程(可能通过cmd.exe运行)程序。

I don't know if the purpose of your code is to define environment variables in a persistent manner, but if that's the case it can't be done in Java without specific OS tools and in a platform-independent way.

我不知道你的代码的目的是以持久的方式定义环境变量,但如果是这种情况,如果没有特定的OS工具并且与平台无关的方式,它就无法在Java中完成。

Environment variables default values must be stored by system tools in places where they are then read by the very same system tools from. On Linux there are files like .profile and .bashrc in the user home, while on Windows you have the registry. For example on my Windows 7 I have: my PATH default value stored in \HKEY_CURRENT_USER\Environment - so you must find a way to write to the system registry, for example the command reg

环境变量默认值必须由系统工具存储在可以通过相同系统工具读取的位置。在Linux上,用户家中有.profile和.bashrc等文件,而在Windows上则有注册表。例如,在我的Windows 7上,我有:我的PATH默认值存储在\ HKEY_CURRENT_USER \ Environment中 - 所以你必须找到一种写入系统注册表的方法,例如命令reg

The REG ADD command allows the user to add new keys and values to the Registry. To display the full range of parameters that can be used, type the following into the command line: reg add /?

REG ADD命令允许用户向注册表添加新的键和值。要显示可以使用的所有参数,请在命令行中键入以下内容:reg add /?

To add the key HKLM\Software\MyNewApp on remote computer PC2, type:

要在远程计算机PC2上添加密钥HKLM \ Software \ MyNewApp,请键入:

REG ADD \\PC2\HKLM\Software\MyNewApp

To add a registry entry to HKLM\Software\MyNewApp with a value named Data of type REG_BINARY and data of fe340ead, type:

要使用名为Data类型为REG_BINARY的数据和fe340ead数据的值向HKLM \ Software \ MyNewApp添加注册表项,请键入:

REG ADD HKLM\Software\MyNewApp /v Data /t REG_BINARY /d fe340ead

You can either write a .bat script or call reg from your Java program. You may need administrator priviledges and to restart some programs (eventually the whole machine) for the update to take effect (for example restart explorer.exe)

您可以编写.bat脚本或从Java程序中调用reg。您可能需要管理员权限并重新启动某些程序(最终是整个计算机)才能使更新生效(例如,重新启动explorer.exe)

#2


It's impossible to do that (at least for forever). There are good reasons for that (isolation, java tools unknowingly changing your env). Look for a hack here :

这样做是不可能的(至少是永远的)。这有很好的理由(隔离,java工具在不知不觉中改变你的环境)。在这里找一个黑客:

edit:

a complex explanation was given by raffaele

拉斐尔给出了一个复杂的解释