java 在 Windows 中更改 JAVA_HOME 系统变量的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4338410/
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-10-30 05:57:10  来源:igfitidea点击:

Script to Change JAVA_HOME System Variable in Windows

javascriptingpathsystem-variable

提问by javamonkey79

I do most of my work against JDK 1.5 - but occasionally I have to change to 1.6. it is a bit painful to have to manually go and change my 'JAVA_HOME' system variable whenever I need to work on a project specific to one or the other (and no, Eclipse doesn't play well in these scenarios - trust me...I've tried.)

我的大部分工作都是针对 JDK 1.5 进行的 - 但偶尔我不得不更改为 1.6。每当我需要处理特定于一个或另一个的项目时,不得不手动更改我的“JAVA_HOME”系统变量有点痛苦(不,Eclipse 在这些场景中表现不佳 - 相信我.. 。我试过了。)

I'm looking for a registry script or windows shell script or for that matter any means by which I can "toggle" this system variable with something that is easy to run.

我正在寻找一个注册表脚本或 windows shell 脚本,或者就此而言,我可以通过任何易于运行的东西“切换”这个系统变量。

I've messed with the 'set' command, but that only sets the variable for that particular command instance - not globally.

我弄乱了“设置”命令,但这只是为该特定命令实例设置变量 - 而不是全局。

Thanks in advance.

提前致谢。

EDIT #1: Points of advise:

编辑#1:建议要点:

  • Use the JAVA_HOMEvariable in your path variable as well, that way you only have to change the JAVA_HOME(which is used in many projects anyways [maven, ant, etc])
  • Write the command into a couple batch scripts for easy use
  • When you make the change the windows command session will not reflect it right away. You must close and reopen it.
  • 在您的路径变量中也使用JAVA_HOME变量,这样您只需要更改JAVA_HOME(无论如何在许多项目中使用 [maven、ant 等])
  • 将命令写入几个批处理脚本以方便使用
  • 当您进行更改时,windows 命令会话不会立即反映出来。您必须关闭并重新打开它。

回答by barti_ddu

You could use setxfor that purpose

您可以setx为此目的使用

Like so:

像这样:

setx /M JAVA_HOME "C:\Program Files (x86)\Java\jdk1.6.0_17"