'Switch JDK version in Windows 10 cmd

Is there a way to change JDK version easily in cmd? like the version on mac. Change Default JDK on Mac.



Solution 1:[1]

Here's my guide for Windows 10.

Step 1. Go to System Properties. Click on Environment Variables

Step 2. Add new variables, such as JAVA_8_HOME

  • JAVA_8_HOME:%ProgramFiles%\Java\jdk1.8.0_152\bin
  • JAVA_9_HOME:%ProgramFiles%\Java\jdk-9.0.1\bin
  • JAVA_HOME:%JAVA_8_HOME%

In my case, JAVA_8_HOME(JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin. You can replace this with your own path to javac. %JAVA_HOME% has a default value pointing to JAVA_8_HOME, which is the path for JDK8. That's my preference, feel free to adjust accordingly.

Step 3. Select PATH and click on Edit. PATH

Step 4. Click on New and add %JAVA_HOME%. %JAVA_HOME% will be added to PATH automatically every time you launch a command prompt.


In order to switch JDK version in cmd, here's the trick. Step 5. I created a batch file with

@echo off
:: Switch JDK version
DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%;
DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%

Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, java8 or java9 is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat. You can name it whatever you want.

Step 6. Search for regedit (Registry Editor). Click on Edit > New > String Value. Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data. Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat, which creates those two alias in the script.

Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.

Step 8. Verify your results here.

If you're using different Python versions, same trick applies, too. Find my python environment settings here.

Solution 2:[2]

I created scripts for every version of Java that I have installed on my PC, and use those whenever I need to change the Java version through the command line. Here is an example of one, should be self-explanatory really:

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk-10
echo setting PATH
set PATH=C:\Program Files\Java\jdk-10\bin;%PATH%
echo Display java version
java -version

Solution 3:[3]

Create a folder (in my case it is in C:\devenv) and create a batch file and name it after the version (jdk8.bat, jdk9.bat, jdk10.bat, jdk11.bat etc..) then add this commands. You just need to change the set JAVA_HOME to the location of the jdk version. Add the location of the batch files (C:devenv) to the path on the environment variables

@echo off
set JAVA_HOME=C:\Program Files\Java\jdk-11.0.9
set PATH=%JAVA_HOME%\bin;%PATH%
@echo Display Java Version
java -version

You can run it on cmd by just typing the batch name (e.g jdk11)

Solution 4:[4]

Found a single batch file to be a simpler solution. Adding batch file location path to system environmental variables will allow you to execute it from cmd.

Batch file (javaVer.bat):

@echo off
echo.

IF "%1" == ""       GOTO NoParams
IF "%1" == "8"      GOTO J8
IF "%1" == "11"     GOTO J11
IF "%1" == "latest" GOTO latest

IF "%1" == "-h"         GOTO help
IF "%1" == "--help"     GOTO help

:NoParams
    echo Enter Java version
    GOTO options
    
:help
    echo Usage: javaVer [java version]
    GOTO options

:: available java versions
:options
    echo options: 8, 11, latest.
    GOTO exit

:: java 8
:J8
    set JAVA_HOME=C:\Program Files\Java\jdk8u282-b08
    GOTO activate

:: java 11
:J11
    set JAVA_HOME=C:\Program Files\Java\jdk-11.0.10+9
    GOTO activate

:: latest java version
:latest
    set JAVA_HOME=C:\Program Files\Java\jdk-15.0.2+7
    GOTO activate

:: activates the selected java version
:activate
    set Path=%JAVA_HOME%\bin;%Path%
    echo Java %1 activated.
    GOTO exit

:: prints java version
:exit
    echo.
    echo Current Java version:
    java -version

Usage: javaVer [java version]

exp: javaVer 8

Solution 5:[5]

In "System Properties" window, click “Environment Variables…” and select PATH by click on Edit, then "New" and type ;%JAVA_HOME%\bin

It will write the JAVA_HOME defined. Just to edit one time.

And after you define JAVA_HOME by the cmd commande : set JAVA_HOME = path_of_jdk_or_jre whenever you want.

Solution 6:[6]

I been doing this by replacing JAVA_HOME variable in Environment variables - System Properties only.

Login as admin user if you are a domain user.

Edit Environment variables -> Advanced -> System Variables -> NEW:

a) Add multiple entries for JAVA_HOME as below:-

 JAVA_HOME (in my case, its version 11 is by default),  
 JAVA_HOME_6 (for version 6),  
 JAVA_HOME_7 (for version 7),  
 JAVA_HOME_8 (for version 8)

system variables JAVA_HOME

b) now edit Path under same System variables and add NEW path for java as below. Remove any existing java path entries. enter image description here

Suppose I need to change java version to 8, then i change the key value of JAVA_HOME. i.e, JAVA_HOME renamed to JAVA_HOME_11 and JAVA_HOME_8 renames to JAVA_HOME You need to close existing CMD and open new CMD to verify this change. I admit its still a GUI solution only

If some appliations are still not taking new java path set, then you need to check java configuration in start up

c) Click Configure Java App in windows start up. Here you can enable required path via checkbox. This elminates path issues. enter image description here

Thanks

Solution 7:[7]

Since I use powershell for day to day tasks, here is a function I added to my profile available for usage. I just have two specific versions.

function JavaSetVersion([string] $v) 
{
  $jre8 = 'C:\Program Files\Eclipse Adoptium\jre-8.0.322.6-hotspot'
  $jdk8 = 'C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot'
  $jdk11 = 'C:\Program Files\Eclipse Adoptium\jdk-11.0.14.101-hotspot'
  $jdk17 = 'C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot'
  $arrToConcatToPath

  # I *think* 8 requires both JRE & JDK
  if ($v -eq '8')
  {
    [System.Environment]::SetEnvironmentVariable('JAVA_HOME', $jdk8, [System.EnvironmentVariableTarget]::User)
    $arrToConcatToPath = "$jre8\bin","$jdk8\bin"
  }
  elseif ($v -eq '11') {
    [System.Environment]::SetEnvironmentVariable('JAVA_HOME', $jdk11, [System.EnvironmentVariableTarget]::User)
    $arrToConcatToPath = "$jdk11\bin"
  }
  elseif ($v -eq '17') {
    [System.Environment]::SetEnvironmentVariable('JAVA_HOME', $jdk17, [System.EnvironmentVariableTarget]::User)
    $arrToConcatToPath = "$jdk17\bin"
  }
  else 
  {
    Write-Host 'Unsupported version'
  }

  # Add to path while not continually growing path
  $newPath = [Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User) -split ";" | Where-Object {$_ -notlike '*Eclipse Adoptium*'} | Where-Object {$_ -ne ''}
  # Add appropriate version back to array
  $newPath += $arrToConcatToPath
  # Join array and reset path
  [System.Environment]::SetEnvironmentVariable('PATH', $($newPath -join ";"), [System.EnvironmentVariableTarget]::User)
}

Usage: JavaSetVersion '11'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ramjay Romorosa
Solution 4 Vlad Palnik
Solution 5 daniele3004
Solution 6
Solution 7