如何在 Windows 中设置环境变量(不像听起来那么简单)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4218434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:37:34  来源:igfitidea点击:

How are Environment Variables set in Windows (Not as easy as it sounds)

windowsenvironment-variables

提问by David W.

See Update Below

请参阅下面的更新

Let me explain the situation:

我来解释一下情况:

In the System Control Panel, I setup JAVA_HOMEas C:\Java\JDK1.6. However, when I went into a terminal window to see what's going on, I got this:

在系统控制面板中,我设置JAVA_HOMEC:\Java\JDK1.6. 然而,当我进入终端窗口查看发生了什么时,我得到了这个:

echo %JAVA_HOME%
D:\Program Files\Java\jre6

Where in the hell is this getting set, and why isn't it picking up the value I set it in the control panel? Other environment variables I put in are set, but it looks like something is overriding it.

这到底是在哪里设置的,为什么它不获取我在控制面板中设置的值?我输入的其他环境变量已设置,但看起来有些东西覆盖了它。

Word of Warning:

Although I'm an Administrator on the system, they've put all sorts of goofy restrictive policies on it. For example, I can't set my Recycle Bin not to warn me when I delete something. The Propertysetting isn't there when you right click on the Recycling Bin. I also can't do regedit. To set the environmental properties, I setup a MCC Console, and pull up the System Control Panel thorough there.

警告语:

虽然我是系统管理员,但他们对它设置了各种愚蠢的限制策略。例如,我无法将回收站设置为在删除某些内容时不发出警告。Property当您右键单击回收站时,该设置不存在。我也做不到regedit。为了设置环境属性,我设置了一个 MCC 控制台,并在那里拉出系统控制面板。



Update

更新

Yes, I know the environment variables don't get reset until I open a new command line prompt. However, this is something I set a couple weeks ago, and the machine had been booted a few times since.

是的,我知道在我打开一个新的命令行提示符之前环境变量不会被重置。然而,这是我几周前设置的,从那以后机器已经启动了几次。

I have it set in the System Variables (the lower box). If I put it in the User Variables (the upper box), it does get set correctly, but my PATHis set incorrectly if I put %JAVA_HOME% in the path.

我在系统变量(下部框)中设置了它。如果我把它放在用户变量(上框)中,它确实设置正确,但PATH如果我把 %JAVA_HOME% 放在路径中,我的设置不正确。

回答by David W.

Going back through my questions and tying up all the loose ends...

回顾我的问题并解决所有未解决的问题......

Turned out that the administrators disabled the ability to set PATH on the system. the admins had a policy that if they didn't understand something, they locked it down. And, since they didn't understand much, they locked everything down.

原来,管理员禁用了在系统上设置 PATH 的能力。管理员有一项政策,如果他们不明白某事,他们就会将其锁定。而且,由于他们不太了解,所以他们锁定了一切。

This was a government office, and the admins locked down anything that didn't involve writing bureaucratic regulations or memos in Microsoft Office that no one bothers to read.

这是一个政府办公室,管理员锁定了任何不涉及在 Microsoft Office 中编写官僚主义法规或备忘录而没有人愿意阅读的内容。

Unfortunately, I was a developer there, so I kept running into these walls. My supervisor quit, and took me to his new site. I'm glad I'm out of there.

不幸的是,我是那里的一名开发人员,所以我一直在撞墙。我的主管辞职了,带我去了他的新网站。我很高兴我离开了那里。

回答by zdan

When you change the environment variable via the control panel, it only changes the environment in the process that it is running.

当您通过控制面板更改环境变量时,它只会更改正在运行的进程中的环境。

When a process starts it will "inherit" the environment of the parent at the time it was launched. Changes afterwards are not propagated to child processes.

当一个进程启动时,它将“继承”启动时父进程的环境。之后的更改不会传播到子进程。

You may have to relaunch your terminal, or to be sure, log off then back on.

您可能需要重新启动您的终端,或者确定要先注销然后再重新启动。

回答by Dirk Vollmar

You have to open a new command prompt to actually "see" the new variable (or call the setcommand in the cmd window for temporary changes or setxfor permanent changes).

您必须打开一个新的命令提示符才能实际“查看”新变量(或set在 cmd 窗口中调用命令进行临时更改或setx永久更改)。

Environment variables are inherited from the parent process when a process is started. When you change or add a variable in the Control Panel, your shell (i.e. the main explorer.exe process) gets to see the change immediately, but not any other already running process. When you start a new process with explorer.exe as the parent process, e.g. by double-clicking a file or chosing Start+Run, the newly created process will also see the updated environment variable(s).

进程启动时,环境变量从父进程继承。当您在控制面板中更改或添加变量时,您的外壳程序(即主 explorer.exe 进程)会立即看到更改,但不会看到任何其他已在运行的进程。当您使用 explorer.exe 作为父进程启动新进程时,例如通过双击文件或选择“开始+运行”,新创建的进程还将看到更新的环境变量。

A nice tool for analyzing such kind of things is Process Explorer. It shows you the relationship between parent and child processes and double-clicking a running process shows you a detailed dialog with a tab listing all environment variables of the current process.

Process Explorer是分析这类事情的一个很好的工具。它显示了父进程和子进程之间的关系,双击正在运行的进程会显示一个详细的对话框,其中包含一个列出当前进程的所有环境变量的选项卡。

回答by user3366459

The problems is, all executables are in binfolder so you should set %JAVA_HOME%\bininside your PATHvariable.

问题是,所有可执行文件都在bin文件夹中,因此您应该%JAVA_HOME%\binPATH变量中进行设置。

You were almost there my friend, it usually happens. Marce

你快到了,我的朋友,这通常会发生。马斯