在Windows 10 cmd中切换JDK版本 [英] Switch JDK version in Windows 10 cmd
问题描述
有没有办法在cmd中轻松更改JDK版本?喜欢mac上的版本。
在Mac上更改默认JDK 。
Is there a way to change JDK version easily in cmd? like the version on mac. Change Default JDK on Mac.
推荐答案
这是我的Windows 10指南。
Here's my guide for Windows 10.
步骤1.转到系统属性。点击环境变量
步骤2.添加新变量,例如 JAVA_8_HOME
Step 2. Add new variables, such as JAVA_8_HOME
-
JAVA_8_HOME
:%ProgramFiles%\ Java \ jdk1.8.0_152 \ bin
-
JAVA_9_HOME
:%ProgramFiles%\ Java \ _jdk-9.0.1 \ bin
-
JAVA_HOME
:%JAVA_8_HOME%
JAVA_8_HOME
:%ProgramFiles%\Java\jdk1.8.0_152\bin
JAVA_9_HOME
:%ProgramFiles%\Java\jdk-9.0.1\bin
JAVA_HOME
:%JAVA_8_HOME%
在我的情况下, JAVA_8_HOME
(JDK8)指向 C:\Program Files\Java \ jdk1.8.0_152\bin
。您可以使用自己的 javac
路径替换它。 %JAVA_HOME%
的默认值指向 JAVA_8_HOME
,这是JDK8的路径。这是我的偏好,可以随意调整。
In my case, JAVA_8_HOME
(JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin
. You can replace this with your own path to javac
. %JAVA_HOME%
has a default value pointing to JAVA_8_HOME
, which is the path for JDK8. That's my preference, feel free to adjust accordingly.
步骤3.选择 PATH
并点击修改
。 PATH
Step 3. Select PATH
and click on Edit
. PATH
第4步。点击新
并添加%JAVA_HOME% 。每次启动命令提示符时,%JAVA_HOME%
将自动添加到 PATH
。
Step 4. Click on New
and add %JAVA_HOME%. %JAVA_HOME%
will be added to PATH
automatically every time you launch a command prompt.
为了在cmd中切换JDK版本,这就是诀窍。
步骤5.我创建了一个批处理文件,其中
In order to switch JDK version in cmd, here's the trick. Step 5. I created a batch file with
@echo off
:: Switch JDK version
DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%;
DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
基本上,它禁用echo并创建两个别名。在批处理文件中, ::
之后的任何字符串都是注释。每次调用 java8
或 java9
时,它都会使用新的JDK路径重新导出%PATH%。将其另存为 profile.bat
。您可以随意命名。
Basically, it disables echo and creates two alias. In batch file any string after ::
is the comments. Every time, java8
or java9
is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat
. You can name it whatever you want.
步骤6.
搜索 regedit
(注册表编辑器)。单击编辑
> 新
> 字符串值
。将 AutoRun
作为值名称
和%USERPROFILE%\ profile.bat
作为值数据
。在这里,请将您的实际路径值放到我们刚刚创建的 profile.bat
中。因此,每当打开命令提示符时,它会自动加载 profile.bat
,这会在脚本中创建这两个别名。
Step 6.
Search for regedit
(Registry Editor). Click on Edit
> New
> String Value
. Give AutoRun
as the Value name
and %USERPROFILE%\profile.bat
as the Value data
. Here, please put your actual path value to the profile.bat
we just created. So, whenever a command prompt is opened, it automatically loads profile.bat
, which creates those two alias in the script.
步骤7.关闭您正在使用的任何命令提示符,或者只是打开一个新的命令提示符。这是因为您的更改不会影响打开的cmd窗口。环境变化只发生在新的CMD上。
Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
步骤8.验证您的结果此处。
Step 8. Verify your results here.
如果您使用的是不同的Python版本,同样的技巧也适用。找到我的python环境设置此处。
If you're using different Python versions, same trick applies, too. Find my python environment settings here.
这篇关于在Windows 10 cmd中切换JDK版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!