从 Windows 批处理文件设置系统环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803581/
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
Setting a system environment variable from a Windows batch file?
提问by Santhosh
Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.
是否可以从 Windows 7(甚至 XP)中的命令提示符在系统级别设置环境变量。我从提升的命令提示符运行。
When I use the set
command (set name=value
), the environment variable seems to be only valid for the session of the command prompt.
当我使用set
命令 ( set name=value
) 时,环境变量似乎只对命令提示符会话有效。
采纳答案by Hugh Allen
The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe
:
XP 支持工具(可以从 XP CD 安装)带有一个名为setx.exe
:
C:\Program Files\Support Tools>setx /?
SETX: This program is used to set values in the environment
of the machine or currently logged on user using one of three modes.
1) Command Line Mode: setx variable value [-m]
Optional Switches:
-m Set value in the Machine environment. Default is User.
...
For more information and example use: SETX -i
I think Windows 7 actually comes with setx
as part of a standard install.
我认为 Windows 7 实际上是setx
作为标准安装的一部分提供的。
回答by Mindaugas Jaraminas
Simple example for how to set JAVA_HOME with setx.exe
in command line:
如何setx.exe
在命令行中设置 JAVA_HOME 的简单示例:
setx JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
This will set environment variable "JAVA_HOME" for current user. If you want to set a variable for all users, you have to use option "-m". Here is an example:
这将为当前用户设置环境变量“JAVA_HOME”。如果你想为所有用户设置一个变量,你必须使用选项“-m”。下面是一个例子:
setx -m JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
Note: you have to execute this command as Administrator.
注意:您必须以管理员身份执行此命令。
Note: Make sure to run the command setxfrom an command-line Admin window
注:确保运行命令SETX从一个命令行管理窗口
回答by Anton F.
If you set a variable via SETX, you cannot use this variable or its changes immediately. You have to restart the processes that want to use it.
如果通过 SETX 设置变量,则不能立即使用此变量或其更改。您必须重新启动想要使用它的进程。
Use the following sequence to directly set it in the setting process too(works for me perfectly in scripts that do some init stuff after setting global variables):
使用以下序列也可以在设置过程中直接设置它(在设置全局变量后执行一些初始化操作的脚本中对我来说非常有用):
SET XYZ=test
SETX XYZ test
回答by Himanshu Singh
System variables can be set through CMD and registry For ex. reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
系统变量可以通过 CMD 和注册表设置 例如。reg 查询“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment”/v PATH
All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.
这里给出了所有常用的 CMD 代码和系统变量:Set Windows system environment variables using CMD。
Open CMD and type Set
打开 CMD 并输入Set
You will get all the values of system variable.
您将获得系统变量的所有值。
Type set javato know the path details of java installed on your window OS.
键入set java以了解安装在 Windows 操作系统上的 java 的路径详细信息。
回答by FractalSpace
For XP, I used a (free/donateware) tool called "RAPIDEE" (Rapid Environment Editor), but SETX is definitely sufficient for Win 7 (I did not know about this before).
对于 XP,我使用了一个名为“RAPIDEE”(快速环境编辑器)的(免费/捐赠软件)工具,但是对于 Win 7,SETX 绝对足够了(我之前不知道这个)。
回答by abort
Just in case you would need to delete a variable, you could use SETENV from Vincent Fatica available at http://barnyard.syr.edu/~vefatica. Not exactly recent ('98) but still working on Windows 7 x64.
以防万一您需要删除变量,您可以使用 Vincent Fatica 的 SETENV,网址为http://barnyard.syr.edu/~vefatica。不完全是最近的 ('98) 但仍在 Windows 7 x64 上工作。