[英]How do I set a system environment variable in Windows XP without using the “My Computer” Dialog
I'm switching between different Java SDKs (1.4.2, 1.5.0 and 1.6.0) for various projects. 我正在为各种项目切换不同的Java SDK(1.4.2,1.5.0和1.6.0)。 I would like to set the JAVA_HOME environment variable on my Windows XP machine without going through the tedious My Computer -> Advanced -> [Select System Variable] -> Edit -> Ok -> Ok 我想在我的Windows XP机器上设置JAVA_HOME环境变量,而无需通过繁琐的我的电脑 - >高级 - > [选择系统变量] - >编辑 - >确定 - >确定
Is it possible to do this from the command line, or is there a utility that can do this? 是否可以从命令行执行此操作,或者是否有可以执行此操作的实用程序?
(Note that I am not referring to the standard batch file "SET" command - I want to set the environment variable "globally," not just for the life of a console window). (请注意,我不是指标准批处理文件“SET”命令 - 我想要“全局”设置环境变量,而不仅仅是控制台窗口的生命周期)。
From http://vlaurie.com/computers2/Articles/environment.htm : 来自http://vlaurie.com/computers2/Articles/environment.htm :
Using the add-on tool Setx.exe 使用附加工具Setx.exe
It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools . 它不是标准Windows XP安装程序的一部分,但Windows XP Service Pack 2支持工具中包含一个名为setx.exe的命令行工具 。 This tool extends the set command so that permanent changes in the environment variables can be made. 此工具扩展了set命令,以便可以对环境变量进行永久性更改。 For example, to add a folder C:\\New Folder to the path, the command would be setx path "%PATH%;C:\\New Folder" 例如,要将文件夹C:\\ New Folder添加到路径,该命令将是setx path“%PATH%; C:\\ New Folder”
Service Pack 2 Support Tools has a tool called "setx.exe" that can do what you are looking for. Service Pack 2支持工具有一个名为“setx.exe”的工具,可以满足您的需求。 setx path "%PATH%;C:\\New Folder" setx路径“%PATH%; C:\\ New Folder”
Here's some VBScript I use for this: 这是我用于此的一些VBScript:
set args = WScript.Arguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
' Parse args
select case args.Count
case 0, 1, 2
help
case 3
sVariable = args(0)
sValue = args(1)
sScope = UCase(args(2))
sMode = ""
case 4
sVariable = args(0)
sValue = args(1)
sScope = UCase(args(2))
sMode = UCase(args(3))
end select
select case sScope
case "S"
if sMode = "A" then
sValue = colSystemEnvVars(sVariable) & sValue
end if
colSystemEnvVars(sVariable) = sValue
case "U"
if sMode = "A" then
sValue = colUserEnvVars(sVariable) & sValue
end if
colUserEnvVars(sVariable) = sValue
case else
help
end select
WScript.Quit
'******************************************************************************
Sub help()
WScript.Echo ""
WScript.Echo "Create or update an environment variable."
WScript.Echo ""
WScript.Echo "usage:"
WScript.Echo "======"
WScript.Echo "cscript SetVar.vbs variable value {S|U} [A]"
WScript.Echo ""
WScript.Echo "eg:"
WScript.Echo "==="
WScript.Echo "cscript SetVar.vbs MYVAR 'Hello world' U"
WScript.Echo "cscript SetVar.vbs PATH 'C:\MyPath' S A"
WScript.Quit
End Sub
The scope can be 'S'ystem or 'U'ser. 范围可以是'S'系统或'U'ser。 The last argument, 'A', if present, appends the value to the existing value of the variable (useful for adding a directoy to the PATH system variable). 最后一个参数'A'(如果存在)将该值附加到变量的现有值(对于向PATH系统变量添加directoy非常有用)。
The variables will presist, but you'll have to close then re-open a console to use them. 变量将保持不变,但您必须关闭然后重新打开控制台才能使用它们。 I usually run this from the "Run..." dialog, then open a console. 我通常从“运行...”对话框中运行它,然后打开一个控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.