The JDK includes the JRE which you can launch by using the java executable in the bin folder. You use this executable just like any other executable.

When you type java in the command line it is actually shorthand. It searches your PATH until it finds the first matching java executable. If you want to specify a different java executable you can give the absolute path to the executable.

C:\Users\Avril> "C:\Program Files\Java\jdk-11.0.1\bin\java" -jar path\to\file.jar

You may be wondering, if you've set JAVA_HOME and PATH to point to JDK-11, why does java -version still use Java 8?

Take a look at your PATH and you'll likely find something like C:\ProgramData\Oracle\Java\javapath as one of the first entries (see this). This entry was added automatically when you installed Java 8 and points to the Java 8 executables (java, javaw, and javaws). Since it's before your %JAVA_HOME%\bin entry, it takes precedence. However, ...\javapath doesn't contain javac and that's why javac -version used JAVA_HOME (Java 11).

Answer from Slaw on Stack Overflow
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.

Discussions

How can I switch to Java 8 to Java 21?
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
9
2
November 26, 2025
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
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. More on stackoverflow.com
🌐 stackoverflow.com
When will Java 11 replace Java 8 as the default java?
As of Java 11, there is no longer a JRE, and Oracle's recommended method for distributing a Java application to end users is to bundle a stripped-down JRE along with your application into a single installer. The Java 8 JRE will continue to be supported for a while (I think till end of 2020 IIRC) and will presumably remain available for download on java.com, but I don't think you'll ever see Java 11 there. (Java 9 and 10 aren't in the picture either, since they're not LTE and are both EOL already.) More on reddit.com
🌐 r/java
81
100
November 8, 2018
🌐
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 - XXXX@XXXXX MINGW64 ~ $ java -version java version "11.0.15" 2022-04-19 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.15+8-LTS-149) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.15+8-LTS-149, mixed mode) ... that's because the syntax in Git Bash is different from the Windows command syntax; you basically have to use Linux syntax there.
🌐
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
🌐
Reddit
reddit.com › r/javahelp › how can i switch to java 8 to java 21?
r/javahelp on Reddit: How can I switch to Java 8 to Java 21?
November 26, 2025 -

A couple things to note is that for the Environment Variables, I cannot access the System Part, only the user variables (despite no one else having a user on the PC)
I use windows 11, not sure if there's a difference between 10 and 11 for that

Top answer
1 of 7
4
You didn't say whether or not you are wanting to compile Java using Java 21 JDK, or just run a Java application that requires Java 21 JRE. In either case you don't need to run the windows installer distro of Java. You can download the compressed zip. Open a command window and then go into your use home directory. Unzip the JDK or JRE archive into there. It sounded like you had access to set User Environment variables, as opposed to System ones. So just add a user environment variable, JAVA_HOME, and set it to the directory where you unzipped the archive (e.g., C:\Users\yourUsername\jdk-21_whateverversion).
2 of 7
1
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.
🌐
Oracle
java.com › en › download › help › update_runtime_settings.html
Update Java runtime settings on Windows
Click OK in Java Control Panel window to confirm changes and close the window.
Find elsewhere
🌐
Myrobotlab
myrobotlab.org › content › how-run-java-11-and-java-8-same-machine
How to run Java 11 and Java 8 on the same machine | MyRobotLab
Now type the following in C:\myrobotlab.1.0.2693.16 ... How long does this last ? As long as you keep using the command prompt where you did the set command. It only effects that command prompt AND anything that is started from that command prompt. So right now its a Java 8 command prompt. Lets make a Java 11 command prompt. Start a new command prompt from the windows search
🌐
javaspring
javaspring.net › blog › change-java-version-windows
Changing Java Version on Windows: A Comprehensive Guide — javaspring.net
Change the value to the installation directory of the desired Java version. For example, to switch to Java 11, set the value to C:\Program Files\Java\jdk - 11.0.10.
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
🌐
University of Oregon
service.uoregon.edu › TDClient › KB › ArticleDet
Reverting to a Previous Version of Java (Windows)
July 16, 2017 - Example: For Java 8 Update 60, select the jre-8u60-windows-i586.exe file.
🌐
YouTube
youtube.com › watch
Download and Install Java 21 | Works for Java 8, 11 & 17 and ...
To learn more, please visit the YouTube Help Center: https://www.youtube.com/help
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › migrate › index.html
Java Platform, Standard Edition Oracle JDK Migration Guide, Release 11
October 19, 2021 - For example, if JDK 11.0.1 is installed with JDK 11, then the installer creates the another Windows registry key as shown: ... This section highlights APIs that have been made inaccessible, removed, or altered in their default behavior. You may encounter the issues described in this section when compiling or running your application. See Removed APIs in JDK 11. The following are some important APIs that have been removed from JDK 9 and JDK 10 releases. The Java team is committed to backward compatibility. If an application runs in JDK 8, then it will run on JDK 9 and later releases as long as it uses APIs that are supported and intended for external use.
🌐
Atlassian
developer.atlassian.com › server › framework › atlassian-sdk › how-to-quickly-switch-between-installed-java-versions-in-terminal
How to quickly switch between Java 11, Java 8 and OpenJDK versions
July 20, 2022 - Select the java version you wish to switch to from the following: [0] 1.8.0_192, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home [1] 1.8.0_191, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home [2] 1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home Please chose a value from the options above: 1 JAVA_HOME is set to be /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home
🌐
Oracle
java.com › en › download › help › windows_manual_download.html
How do I manually download and install Java for my Windows computer?
This will download an executable file (example: jre-8u333-windows-i586-iftw.exe) to the default download location on the local system Note: You can opt to save this file to any other known location on your local system for later installation. Double click on executable file to run the installer and this will launch a User Account Control dialog indicating confirmation if you want to allow this app to make changes to your device?
🌐
Onghu
notepad.onghu.com › 2021 › java-windows-multiple-installations
Java on Windows: switching to a specific Java version/ runtime
May 16, 2021 - $ path=d:\apps\jdk\jdk-11.0.11_x64\bin;%PATH% $ set JAVA_HOME=d:\apps\jdk\jdk-11.0.11_x64\ $ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)