@echo off ::Get Administrator Privileges %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit cd /d "%~dp0" ::Setting Window Size mode con cols=120 lines=60 title JDK-Version change script of Bat :menu echo current jdk version: java -version echo. echo ============================================= echo ################ JDK version-list ################ echo. echo [0] cancel switch echo [8] switch to JDK8 echo [11] switch to JDK11 echo [15] switch to JDK15 echo. echo ============================================= echo. set /P vb=Please choose need switch JDK versions: if "%vb%" EQU "8" ( setx "JAVA_HOME" "D:\OpenJdk\jdk1.8" /m echo Tips: Successfully switched JDK version, "JAVA_HOME" has been modified D:\OpenJdk\jdk1.8. pause echo. ) else if "%vb%" EQU "11" ( setx "JAVA_HOME" "D:\OpenJdk\jdk11" /m echo Tips: Successfully switched JDK version, "JAVA_HOME" has been modified D:\OpenJdk\jdk11. pause echo. ) else if "%vb%" EQU "15" ( setx "JAVA_HOME" "D:\OpenJdk\jdk15" /m echo Tips: Successfully switched JDK version, "JAVA_HOME" has been modified D:\OpenJdk\jdk15. pause echo. ) else if "%vb%" EQU "0" ( goto exit ) else ( echo. echo ! choosing version error, please renew choice ... echo. goto menu pause ) echo Please press any key to exit.. & pause > nul :exit
将上述粘贴到.bat文件中,只需要在上述的java version-list 提示列表需要替换的JDK版本进行修改, 再在if else if 判断语句中实际修改你需要切换的JD版本即可。
下面进行详细演示。
首先在Win系统环境变量中创建"JAVA_HOME"
思想: 只需要修改JAVA_HOME的属性值即可修改Java环境(上述bat脚本中 setx "JAVA_HOME" "你JDK文件存在的实际地址" /m)
这里我的所有版本的JDK包括切换的脚本都在我win本的D盘符下。
最后将修改好的Batch File(bat)脚本添加到系统环境变量中,能在powershell或cmd中快速打开。这里我的bat脚本文件叫做 jdkSwitchScript.bat(该脚本已经添加到系统环境中)
使用该bat脚本将Java版本修改为11.
重新打开一个terminal(powershell 或者 cmd)输入java -version
替换完成了,jshell也可以使用啦。