cmd java版本命令,在Windows 10 cmd中切换JDK版本

Is there a way to change JDK version easily in cmd? like the version on mac.

Change Default JDK on Mac.

解决方案

Here's my guide for Windows 10.

Step 1. Go to System Properties. Click on Environment Variables

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%

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.

Step 3. Select PATH and click on Edit. 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.

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%

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.

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.

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.

Step 8. Verify your results here.

If you're using different Python versions, same trick applies, too. Find my python environment settings here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值