As Java developers we are often found to work with various java versions. Here is a windows batch file that would help you switch the Java versions in a more sane way. Switch between different JDK versions in Windows with a batch file.
Before running the Batch file, here are the things to make a note of:
- Ensure all java versions are installed @ “C:\Program Files\Java”, else make that minor change in the provided batch file. The idea is to have all jdk’s installed in one location.
- Set the JAVA_HOME system variable to the appropriate java version
- Ensure the PATH variable has %JAVA_HOME%\bin, as one of the entries
- If you installed Java 8
- \windows\system32 contains Java 8 java.exe, javaw.exe and javaws.exe. Your path probably has system32 near the beginning, so these tend to be run by default. Delete these files.
- The system path variable starts with C:\programdata\Oracle\Java\javapath. This folder contains java.exe, javaw.exe and javaws.exe as symlinks to the JRE 8 executables. Delete these files or remove the entry.
Save the following file as ChangeJDK.bat (or whatever name you choose)
Ensure to run this batch file as an administrator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
@echo off echo "Current Java Version is" echo %JAVA_HOME% setlocal enableDelayedExpansion SET PT="C:\Program Files\Java" cd %PT% ::build "array" of folders set folderCnt=0 for /f "eol=: delims=" %%F in ('dir /b /ad *') do ( set /a folderCnt+=1 set "folder!folderCnt!=%%F" ) ::print menu for /l %%N in (1 1 %folderCnt%) do echo %%N - !folder%%N! echo( :get selection set selection= set /p "selection=Enter number to Set the JAVA_HOME and PATH " echo you picked %selection% - !folder%selection%! cd %PT%\!folder%selection%! echo %cd% ( endlocal & rem return Set jdk=%cd% ) echo Setting JAVA_HOME SETX /M JAVA_HOME "%jdk%" SET JAVA_HOME=%jdk% echo %JAVA_HOME% REM echo setting PATH REM setx PATH %JAVA_HOME%\bin;%PATH% -m REM echo Display java version REM java -version