Change your PATH variable so that it has the location of the jdk5/bin directory:

  1. Start -> Control Panel -> System -> Advanced
  2. Click on Environment Variables, under System Variables, find PATH, and click on it.
  3. In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
  4. Close the window.
  5. Reopen Command prompt window, and run java -version
Answer from dogbane on Stack Exchange
Discussions

Switch between java versions with a command on Windows
So is there a way I can switch between my instaled jdks? For example if I have installed jdk8 and jdk16 I can run a batch file and switch the version… More on reddit.com
🌐 r/javahelp
14
3
September 21, 2021
java - Switching between different JDK versions in Windows - Stack Overflow
So I was wondering if there is any easy way to change it? I found 2 ways, which should solve this problem, but it doesn't work. ... @echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_72 echo setting PATH set PATH=C:\Program Files\Java\jdk1.7.0_72\bin;%PATH% echo Display java version java -version pause · And after running this bat, I see right version of Java. But when I close this CMD ... More on stackoverflow.com
🌐 stackoverflow.com
java - Switch JDK version in Windows 10 cmd - Stack Overflow
Is there a way to change JDK version easily in cmd? like the version on mac. Change Default JDK on Mac. ... 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 More on stackoverflow.com
🌐 stackoverflow.com
java - Setting the JVM via the command line on Windows - Stack Overflow
Is it possible to specify the JVM to use when you call "java jar jar_name.jar" . I have two JVM installed on my machine. I can not change JAVA_HOME as it may break code that is all ready running. ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
HappyCoders.eu
happycoders.eu › java › how-to-switch-multiple-java-versions-windows
How to Change Java Versions in Windows (up to Java 25)
June 11, 2025 - #ask my default java 19 when i change to java8-system then i call java -version 'success' change to java 8 but when i call javac -version 'this version call java 19' how to change java and javac to version i want? thanks before ... please check your path settings as described in the article. You probably have another directory on your path that contains the javac command from Java 8. ... Hi Sven, how do the user and system variants of the scripts function? When attempting to switch java versions, neither of them work. Doing "java8-user" or "java8-master" in the CMD window do not change the version at all, while doing java8 changes the version in the current CMD, but the changes are not saved.
🌐
Reddit
reddit.com › r/javahelp › switch between java versions with a command on windows
r/javahelp on Reddit: Switch between java versions with a command on Windows
September 21, 2021 - Yo wtf, are they stupid or what, but yeah you could have a script for this and just invoke an script every time for an exact version of java aka: runJ8.bat and inside of it you have directory to javac and you are done
Top answer
1 of 9
58

The set command only works for the current terminal. To permanently set a system or user environment variable you can use setx.

You can set the variable for the current user like this:

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72"

You can also set the variable system wide (Note: The terminal must be run as administrator fo this) by running the same command with the /m option:

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72" /m

The variable will be available in all new terminal session, but not the current one. If you also want to use the path in the same session, you need to use both set and setx.

You can avoid manipulating the PATH variable if you just once put %JAVA_HOME%\bin in there, instead of the full JDK path. If you change JAVA_HOME, PATH will be point to the new location too.


There are also a few environment variable editors as alternative to the cumbersome Windows environment variable settings. See "Is there a convenient way to edit PATH in Windows 7?" on Super User.

2 of 9
19

In case if someone want to switch frequently in each new command window then I am using following approach.

Command Prompt Version:

Create batch file using below code. You can add n number of version using if and else blocks.

@echo off
if "%~1" == "11" (
   set "JAVA_HOME=C:\Software\openjdk-11+28_windows-x64_bin\jdk-11"
) else (
   set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151"
)
set "Path=%JAVA_HOME%\bin;%Path%"
java -version

Save this batch file as SJV.bat and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".

Now open new command window and just type SJV 11 it will switch to Java 11. Type SJV 8 it will switch to Java 8.

PowerShell Version

Create powershell(ps1) file using below code. You can add n number of version using if and else blocks.

If($args[0] -eq "11")
{
    $env:JAVA_HOME = 'C:\Software\openjdk-11+28_windows-x64_bin\jdk-11'
}else{
    $env:JAVA_HOME = 'C:\Program Files\Java\jdk1.8.0_151'
}
$env:Path = $env:JAVA_HOME+'\bin;'+$env:Path
java -version

Save this script file as SJV.ps1 and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".

Now open new powershell window and just type SJV 11 it will switch to Java 11. Type SJV 8 OR SJV it will switch to Java 8.

I hope this help someone who want to change it frequently.

🌐
JRebel
jrebel.com › blog › switching-java-versions-command-line
Switching Java Versions on the Command Line | JRebel by Perforce
August 30, 2017 - To run Java command line utilities successfully, including the java command, you need two things: ... Here's how I do it, and if I'm not mistaken I took this approach from Neeme. We'll utilize the ~/.bashrc file to declare the functions we'll use later. So open it in your favorite editor, something line atom ~/.bashrc would do. The first function, which we'll use later to set the JDK versions is setjdk. # set and change java versions function setjdk() { if [ $# -ne 0 ]; then removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin' if [ -n "${JAVA_HOME+x}" ]; then removeFromPath $JAVA_HOME fi export JAVA_HOME=`/usr/libexec/java_home -v $@` export PATH=$JAVA_HOME/bin:$PATH fi }
🌐
Medium
medium.com › @pabasarapasindu365 › effortlessly-switch-between-multiple-java-versions-in-the-same-command-prompt-on-windows-03adc8d1e96e
Effortlessly Switch Between Multiple Java Versions in the Same Command Prompt on Windows.
July 18, 2024 - In this tutorial, I’ll show you how to effortlessly switch between multiple Java versions in the same command prompt using simple batch scripts.
Find elsewhere
Top answer
1 of 7
16

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.

2 of 7
5

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
Top answer
1 of 4
48

Yes - just explicitly provide the path to java.exe. For instance:

c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_03\bin\java.exe" -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_12\bin\java.exe" -version
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

The easiest way to do this for a running command shell is something like:

set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%

For example, here's a complete session showing my default JVM, then the change to the path, then the new one:

c:\Users\Jon\Test>java -version
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

c:\Users\Jon\Test>set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%

c:\Users\Jon\Test>java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

This won't change programs which explicitly use JAVA_HOME though.

Note that if you get the wrong directory in the path - including one that doesn't exist - you won't get any errors, it will effectively just be ignored.

2 of 4
2

You should be able to do this via the command line arguments, assuming these are Sun VMs installed using the usual Windows InstallShield mechanisms with the JVM finder EXE in system32.

Type java -help for the options. In particular, see:

-version:<value>
              require the specified version to run
-jre-restrict-search | -jre-no-restrict-search
              include/exclude user private JREs in the version search
🌐
Chetangole
chetangole.com › blogs › change current java version in windows 10 in one click - using a batch file
Change Current Java Version in Windows 10 in one click - using a batch file | Chetan Gole
@echo off echo Current Java is echo %JAVA_HOME% echo Setting new Java version as setx -m JAVA_HOME "C:\Program Files\Java\jdk<replace_your_version>" echo %JAVA_HOME% echo Restart the session pause · Right-click the bat and Run as Administrator, restart or start a new command or power shell session to take effect. Create as many batch files as per your Java version needs.
🌐
Baeldung
baeldung.com › home › installation › switch between multiple java versions
Switch Between Multiple Java Versions | Baeldung on Linux
March 18, 2024 - Afterwards, we’ll enlist the versions again to verify that the JDK has changed successfully: $ update-alternatives --config java There are 3 choices for the alternative java (providing /usr/bin/java).
🌐
Ultimate Systems Blog
blog.usro.net › ultimate systems blog › how-to › windows › how to change, set, check, and update java on windows
How to Change, Set, Check, and Update Java on Windows – Ultimate Systems Blog
November 17, 2024 - Check Installed Versions: Locate all installed Java versions. These are usually in C:\Program Files\Java. ... Press Win + S and search for Environment Variables. Under System Variables, find Path and click Edit.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › windows-cmd.html
Java and the Windows Command Prompt
You may also need to reboot for the environment variable change to take effect. I can compile with javac, but I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld" when I try to execute it with java. First, be sure that HelloWorld.class is now in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Check that the command "java -version...
🌐
DEV Community
dev.to › zeagur › running-multiple-version-of-jdk-in-windows-commandline-the-fun-way-4goj
Running multiple version of JDK in Windows commandLine, the fun? way - DEV Community
September 27, 2022 - PS C:\Users\Rujra> $env:PATH %JAVA_HOME%\bin;[other PATH env blah blah blah] PS C:\Users\Rujra> java -version openjdk version "17.0.4.1" 2022-08-12 OpenJDK Runtime Environment Temurin-17.0.4.1+1 (build 17.0.4.1+1) OpenJDK 64-Bit Server VM Temurin-17.0.4.1+1 (build 17.0.4.1+1, mixed mode, sharing) PS C:\Users\Rujra> $env:JAVA_HOME PS C:\Users\Rujra> java11 PS C:\Users\Rujra> $env:PATH C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot\bin;[other PATH env blah blah blah] PS C:\Users\Rujra> $env:JAVA_HOME C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot PS C:\Users\Rujra> java -version openjdk version "11.0.16.1" 2022-08-12 OpenJDK Runtime Environment Temurin-11.0.16.1+1 (build 11.0.16.1+1) OpenJDK 64-Bit Server VM Temurin-11.0.16.1+1 (build 11.0.16.1+1, mixed mode) now it's working, a bit easier to config than CMD for sure, cheers 🥂
🌐
Damir's Corner
damirscorner.com › blog › posts › 20251226-SwitchingBetweenJdksInWindows.html
Switching between JDKs in Windows | Damir's Corner
December 26, 2025 - ➜ Set-Jdk23 ➜ java -version openjdk version "23.0.1" 2024-10-15 OpenJDK Runtime Environment (build 23.0.1+11-39) OpenJDK 64-Bit Server VM (build 23.0.1+11-39, mixed mode, sharing) When I change the JDKs installed through IntelliJ IDEA, I only need to update the functions in my $PROFILE to match the set of installed JDKs and their paths.
🌐
javaspring
javaspring.net › blog › java-version-cmd
Java Version in Command Prompt: A Comprehensive Guide — javaspring.net
Move the directory containing the desired Java version's bin folder to the top of the list. Click "OK" to save the changes.