Installing Multiple Java

  1. Install two or more java as per your requirements. Here I installed Java 8 and Java 11. Java versions
  2. Add %JAVA_HOME%\bin value in path environment variable. Java environment variable Important: Don't add JAVA_HOME environment variable. Batch script will handle this environment variable.

Batch Script

  1. Copy below batch script and save it with the extension of .bat at your convenient location. I placed this batch script at my desktop.
@echo off

REM =========ADD YOUR JAVA HOME==============
set Arr[0]=C:\Program Files\Java\jdk1.8.0_241
set Arr[1]=C:\Program Files\Java\jdk-11.0.8

REM =========================================

set "x=0"
set "choice=1"

echo Existing Path: %JAVA_HOME%
echo Choose Your Java Version:

REM ==========Printing Choices Starts========
if not defined Arr[%x%] goto :endLoop
call set VAL=%%Arr[%x%]%%
echo [%choice%]. %VAL%
REM do your stuff VAL
SET /a "x+=1"
SET /a "choice+=1"
GOTO :SymLoop

echo [x]. Exit / Cancel
REM ==========Printing Choices Ends==========

REM ==========Getting Choices From User======
SET /P C=Your Choice:  

SET /a "C=C-1"

REM ==========Validating Input Starts=========
if not defined Arr[%C%] goto :byebye
for %%? in (C) do if /I "%C%"=="%%?" goto :byebye
REM ==========Validating Input Ends===========

REM ==========Setting Environment=============
@echo off 
call set NEW_PATH=%%Arr[%C%]%%
echo Setting path: %NEW_PATH%
REM ==========Setting Environment Done=========

echo Done.
  1. Edit the batch script in your text editor and add your Java home location as like below. Java batch edit
  2. Save the file and run the script
  3. Select your Java version choice in the command prompt. Java batch run
  4. That's it. Now open another command prompt and enter command java -version
  5. You can see the java version has changed. Java version check