Last active
August 10, 2023 17:16
-
-
Save rwunsch/d157d5fe09e9f7cdc858cec58c8462d6 to your computer and use it in GitHub Desktop.
Windows - Change Java version (JDK) per command line via batch file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal | |
REM Check for elevated priviledges | |
net session >nul 2>&1 | |
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE | |
REM Check if any parameters have been given. | |
if "%*"=="" ( | |
echo No version parameter given. | |
echo Usage: Use the command 'jdk version', eg. 'jdk 1.6' | |
echo Possible options: 6, 7, 8, 11, 17 | |
echo --------- | |
echo Choose Option now: | |
echo 1. for JDK 6 | |
echo 2. for JDK 7 | |
echo 3. for JDK 8 | |
echo 4. for JDK 11 | |
echo 5. for JDK 17 | |
CHOICE /C 12345 /M "Enter your choice:" | |
:: Note - list ERRORLEVELS in decreasing order | |
IF ERRORLEVEL 5 ( | |
set PARAM=17 | |
) | |
IF ERRORLEVEL 4 ( | |
set PARAM=11 | |
) | |
IF ERRORLEVEL 3 ( | |
set PARAM=8 | |
) | |
IF ERRORLEVEL 2 ( | |
set PARAM=7 | |
) | |
IF ERRORLEVEL 1 ( | |
set PARAM=6 | |
) | |
) else ( | |
set PARAM=%1 | |
) | |
REM echo PARAM = %PARAM% | |
if "%PARAM%"=="6" ( | |
set JAVA_VERSION=jdk1.6.0_45 | |
) else ( | |
if "%PARAM%"=="7" ( | |
set JAVA_VERSION=jdk1.7.0_79 | |
) else ( | |
if "%PARAM%"=="8" ( | |
set JAVA_VERSION=jdk-1.8 | |
) else ( | |
if "%PARAM%"=="11" ( | |
set JAVA_VERSION=jdk-11 | |
) else ( | |
if "%PARAM%"=="17" ( | |
set JAVA_VERSION=jdk-17.0.2 | |
) else ( | |
echo ERROR: Version is not set according to available options - see options below | |
echo Usage: Use the command "jdk version", eg. "jdk 1.6" | |
echo Possible options: 6, 7, 8, 11, 17 | |
exit /b | |
))))) | |
echo ---------------------------- | |
echo JAVA-Version: %JAVA_VERSION% | |
echo ---------------------------- | |
echo Trying to remove current JAVA path from PATH | |
echo (Using tool "pathed" - http://p-nand-q.com/download/gtools/pathed.html) | |
pathed /REMOVE "%JDK_HOME%\bin" /MACHINE | |
echo ---------------------------- | |
echo setting PATH unsing "pathed" | |
pathed /APPEND "C:\Program Files\Java\%JAVA_VERSION%\bin" /MACHINE | |
echo ---------------------------- | |
echo Removing duplicates from PATH | |
pathed /SLIM /MACHINE | |
echo ---------------------------- | |
echo Setting JAVA_HOME | |
set JAVA_HOME="C:\Program Files\Java\%JAVA_VERSION%" | |
setx JAVA_HOME "C:\Program Files\Java\%JAVA_VERSION%" /m | |
echo ---------------------------- | |
echo Setting JDK_HOME | |
set JDK_HOME="C:\Program Files\Java\%JAVA_VERSION%" | |
setx JDK_HOME "C:\Program Files\Java\%JAVA_VERSION%" /m | |
echo Display java version | |
java -version | |
SLEEP 5 | |
@ECHO on | |
EXIT /B 0 | |
:ELEVATE | |
ECHO Elevated privileges are required to run script. | |
ECHO Aborting | |
SLEEP 5 | |
@ECHO on | |
EXIT /B 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Windows - Change Java version per batch command | |
Put these batch files in one of the PATH folders. | |
I usually put the %userprofile% in the path and place the BAT files there. | |
To change the JAVA version, type e.g."jdk 1.6". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @Aquaotter ,
What do you mean?