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.

Answer from kapex 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.

🌐
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.
Discussions

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
How to change default Java version in windows-latest?
Select Topic Area Question Body I have a pkg that depends on Java 11 and checks that JAVA_HOME is set and the the command java is at least version 11. I see that the windows-latest comes with Java ... More on github.com
🌐 github.com
2
2
May 10, 2024
java - Switch JDK version in Windows 10 cmd - Stack Overflow
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) ... In "System Properties" window... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
Find elsewhere
🌐
UMA Technology
umatechnology.org › home › how to change java version windows 11
How to change java version Windows 11 - UMA Technology
March 5, 2025 - Changing the Java version on Windows 11 is essential for developers and users who run applications requiring specific Java versions. By following the steps detailed in this guide, you can manage and switch between different Java installations effectively.
🌐
GeekChamp
geekchamp.com › home › how to update java version in windows 11 using cmd
How to update Java version in Windows 11 using CMD - GeekChamp
May 16, 2026 - set JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-8.0.402.6-hotspot set PATH=%JAVA_HOME%\bin;%PATH% java -version · This changes Java only for the current Command Prompt window. If you have multiple JDKs installed and want to test one without changing system settings, use temporary environment variables. ... set JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot set PATH=%JAVA_HOME%\bin;%PATH% java -version javac -version
🌐
Oracle
java.com › en › download › help › update_runtime_settings.html
Update Java runtime settings on Windows
Verify that the latest Java Runtime version is enabled by checking the Enabled box. ... Click OK in Java Control Panel window to confirm changes and close the window.
🌐
GitHub
github.com › orgs › community › discussions › 123161
How to change default Java version in windows-latest? · community · Discussion #123161
May 10, 2024 - But after reading this, I now see that it detects a pre-installed version on windows-latest and allows you to switch to it. I only need: - name: configure_java uses: actions/setup-java@v4 with: distribution: 'temurin' java-version: '11' - name: validate java config shell: powershell run: | java --version echo $ENV:JAVA_HOME
🌐
javaspring
javaspring.net › blog › change-java-version-windows
Changing Java Version on Windows: A Comprehensive Guide — javaspring.net
For example, to switch to Java 11, set the value to C:\Program Files\Java\jdk - 11.0.10. 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 ...
🌐
javaspring
javaspring.net › blog › how-to-update-java-windows-11
How to Update Java on Windows 11 — javaspring.net
Java can be configured to update automatically, which is a convenient way to ensure that your system always has the latest Java version. Here's how to set up automatic updates: Open the Control Panel on your Windows 11 ...
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
🌐
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.
🌐
TechBloat
techbloat.com › home › how to update java on windows 11: a step-by-step guide
How to Update Java on Windows 11: A Step-by-Step Guide - TechBloat
July 3, 2025 - Restart your Command Prompt or PowerShell window to apply changes. To ensure the installation was successful, verify your Java version again:
🌐
GitHub
github.com › FelixSelter › JEnv-for-Windows
GitHub - FelixSelter/JEnv-for-Windows: Change your current Java version with one line · GitHub
Change your java version for the current session jenv use <name> Example: jenv use jdk15 Environment var for scripting: ---PowerShell: $ENV:JENVUSE="jdk17" ---CMD/BATCH: set "JENVUSE=jdk17"
Starred by 916 users
Forked by 106 users
Languages   PowerShell 97.1% | Batchfile 2.9%
🌐
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 - @echo off set JDK_VERSION=%~1 if "%JDK_VERSION%" == "" ( goto usage ) else if "%JDK_VERSION%" == "8" ( set JAVA_HOME="C:\Program Files\Eclipse Adoptium\jdk-8.0.345.1-hotspot" ) GOTO :eof :usage ECHO Please select your Java Version ECHO Usage: useJDK [version] EXIT /B 1 · change the JAVA_HOME path to suit your need and add addtional condition if you have more than 2 JDK ... C:\Users\Rujra>usejdk 11 C:\Users\Rujra>echo %JAVA_HOME% "C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot" C:\Users\Rujra>java -version openjdk version "1.8.0_345" OpenJDK Runtime Environment (Temurin)(build 1.8.0_345-b01) OpenJDK 64-Bit Server VM (Temurin)(build 25.345-b01, mixed mode) C:\Users\Rujra>
🌐
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) (Notice how the last line shows 64-Bit Server VM since it’s now running the 64-bit VM) It’s not as convenient as using pik to switch Ruby versions on Windows but I imagine it would be a good idea to write something like pik for this.
🌐
Java With Us
javawithus.com › home › faq › how to update java on windows 11
How to Update Java on Windows 11 | Java With Us
April 21, 2026 - Close open terminals before verifying — PATH changes only take effect in new windows. ... This lists all packages with available updates. Look for entries with "Adoptium" or "Microsoft" in the source column and upgrade individually or all at once: ... Download the new MSI from adoptium.net (Java 21, Windows x64). Close any terminals, IDEs, or apps using Java. Run the MSI — it upgrades in place, keeping your PATH and JAVA_HOME intact. Open a new PowerShell window and verify: java -version.