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
๐ŸŒ
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 - But don't worry โ€“ the scripts you can download in the next step will do that automatically. Now open a command line to check the settings with the following commands: echo %JAVA_HOME% java -versionCode language: plaintext (plaintext)
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 - Switch JDK version in Windows 10 cmd - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Setting the JVM via the command line on Windows - Stack Overflow
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: require the specified version to run ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I switch between Java versions?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
13
11
March 20, 2023
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 }
๐ŸŒ
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 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
Find elsewhere
๐ŸŒ
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 - Locate Your Java Installation Directory: Typically found in C:\Program Files\Java\jdk-XX, where XX is your JDK version. ... Press Win + S, type Environment Variables, and open it. Under System Variables, click New.
๐ŸŒ
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 - Cmder itself has many features off-the-shelf including linux commands that you can use alongside your normal windows commands whether it's ls -lha dir etc, but now I'll add a bit more power to it by adding ability to dynamically switch JDK version with alias. ! important: before we proceed please make sure you have set the PATH variable with %JAVA_HOME%\bin at the top like we did in CMD or Powershell steps
๐ŸŒ
LogicBig
logicbig.com โ€บ how-to โ€บ java-command โ€บ change-jdk-from-cmd.html
How to change JDK from command line in Windows?
May 20, 2018 - Java Tools & Commands Java ยท To use different JDK than the one which is on the default path. Use followings:
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.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Change Java JDK version in environment variable - Windows 11 Tutorial (2023) - YouTube
In this video, I'll show you how you can change or switch jdk version on windows 11. Adding jdk/java to environment allows you to run java/jdk from command l...
Published ย  November 24, 2023
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ change-java-version-windows
Changing Java Version on Windows: A Comprehensive Guide โ€” javaspring.net
In the same "Environment Variables" window, find the PATH variable and click "Edit". Locate the entry that points to the bin directory of the old Java version and replace it with the bin directory of the new Java version. For Java 11, it would be C:\Program Files\Java\jdk - 11.0.10\bin. Open a new Command Prompt and run the following command:
๐ŸŒ
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 ...
๐ŸŒ
YouTube
youtube.com โ€บ code with arjun
How to switch between the multiple Java versions(JDK) in windows 10 | Switch between java 8,11,15,17 - YouTube
In this Video i am going to show how to switch or manage the multiple java versions of jdk versions at a time in windows operating system. If you have java 8...
Published ย  June 25, 2021
Views ย  145K