1. Download JDK
Go to the official website to download the corresponding jdk version and install it. I have installed three of them: 8, 17, 23.
2. Configure environment variables
1. The first environment variable
Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk-8
2. The second environment variable
Variable name: JAVA8_HOME
Variable value: C:\Program Files\Java\jdk-8
3. The third environment variable
Variable name: JAVA17_HOME
Variable value: C:\Program Files\Java\jdk-17
4. The fourth environment variable
Variable name: JAVA23_HOME
Variable value: C:\Program Files\Java\jdk-23
Create new environment variables in Path,
%JAVA_HOME%\bin
Check the environment variables of Path, add them if there is no, change it to %JAVA_HOME%\bin and move up to the first one
3. Write .bat scripts
The paths of all the guys are changed to their actual paths!!!
@echo off setlocal :menu cls echo ============================ echo Select the JDK version to use: echo ============================ echo 1. JDK 8 (C:\Program Files\Java\jdk1.8.0_191) echo 2. JDK 17 (C:\Program Files\Java\jdk-17) echo 3. JDK 23 (C:\Program Files\Java\jdk-23) echo Q. Quit echo ============================ set /p choice="Enter your choice (1, 2, 3 or Q): " if /i "%choice%"=="Q" goto :eof REM set up JAVA_HOME The path corresponding to the target version if "%choice%"=="1" ( set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_191" ) else if "%choice%"=="2" ( set "JAVA_HOME=C:\Program Files\Java\jdk-17" ) else if "%choice%"=="3" ( set "JAVA_HOME=C:\Program Files\Java\jdk-23" ) else ( echo Invalid choice. Please try again. pause goto :menu ) REM verify JAVA_HOME Does it exist if not exist "%JAVA_HOME%" ( echo The specified JDK path does not exist: %JAVA_HOME% pause exit /b 1 ) REM Update system environment variables JAVA_HOME and Path setx JAVA_HOME "%JAVA_HOME%" /M REM setx PATH "%JAVA_HOME%\bin;%PATH%" /M REM Update the current session JAVA_HOME and Path set "JAVA_HOME=%JAVA_HOME%" REM set "PATH=%JAVA_HOME%\bin;%PATH%" echo Switched to JDK %choice% echo JAVA_HOME is now set to %JAVA_HOME% pause endlocal
4. Administrator permissions to execute .bat file
Execute the .bat file,Reopen a new cmd terminal, and then enter java -version to see if the switch is successful
Summarize
This is all about this article about the quick switching method of JDK version. For more related content on quick switching of JDK version, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!