在Windows中的不同JDK版本之间切换 [英] Switching between different JDK versions in Windows

查看:47
本文介绍了在Windows中的不同JDK版本之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究几个项目,其中一些正在使用不同的JDK.在JDK版本之间切换并不方便.所以我想知道是否有任何简单的方法可以更改它?

I'm working on few projects and some of them are using different JDK. Switching between JDK versions is not comfortable. So I was wondering if there is any easy way to change it?

我发现了两种方法可以解决此问题,但是它不起作用.

I found 2 ways, which should solve this problem, but it doesn't work.

第一个解决方案是创建一个bat文件,如下所示:

First solution is creating a bat files like this:

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_72
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_72\bin;%PATH%
echo Display java version
java -version
pause

在运行了这个bat之后,我看到了正确的Java版本.但是,当我关闭此CMD并打开一个新的CMD并键入"java -version"时,它表示我仍然有1.8.0_25.所以它行不通.

And after running this bat, I see right version of Java. But when I close this CMD and open a new one and type "java -version" it says that I still have 1.8.0_25. So it doesn't work.

我找到的第二个解决方案是来自网站的应用程序.而且它也不起作用.与第一种解决方案相同.

Second solution which I found is an application from this site. And it also doesn't work. The same effect as in the first solution.

有什么想法吗?因为通过以下方法更改JAVA_HOME和PAHT:Win + Pause->高级系统设置->环境变量->并编辑这些变量,是很糟糕的方式...

Any ideas? Because changing JAVA_HOME and PAHT by: Win + Pause -> Advanced System Settings -> Environment Variables -> and editing these variables, is terrible way...

推荐答案

set 命令仅适用于当前终端.要永久设置系统或用户环境变量,可以使用 setx .

The set command only works for the current terminal. To permanently set a system or user environment variable you can use setx.

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72" /m

/m 选项用于设置系统范围的变量(不仅限于当前用户).终端必须以管理员身份运行才能使用此选项.

The /m option is used to set the variable system wide (and not just for the current user). The terminal must be run as administrator to use this option.

该变量将在所有新的终端窗口中可用,但在当前窗口中不可用.如果要在同一窗口中显示路径,则需要同时使用 set setx .

The variable will be available in all new terminal windows, but not the current one. If you want to display the path in the same window, you need to use both set and setx.

如果只在其中放置%JAVA_HOME%而不是完整的JDK路径,则可以避免操纵 PATH 变量.如果更改 JAVA_HOME PATH 也将被更新.

You can avoid manipulating the PATH variable if you just once put %JAVA_HOME% in there, instead of the full JDK path. If you change JAVA_HOME, PATH will be updated too.

除了繁琐的Windows环境变量设置之外,还有一些环境变量编辑器可以替代.请参阅">是否有方便的方法来编辑PATH在Windows 7中?"在超级用户"上.

There are also a few environment variable editors as alternative to the cumbersome Windows environment variable settings. See "Is there a convenient way to edit PATH in Windows 7?" on Super User.

这篇关于在Windows中的不同JDK版本之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆