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.

Discussions

How to change default Java version in windows-latest?
You switched accounts on another tab or window. Reload to refresh your session. ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... 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 ... More on github.com
🌐 github.com
2
2
May 10, 2024
Unable to change Java Version in windows - Stack Overflow
Moreover, it has been prepended to PATH under the Windows env settings. This folder is where java has 3 symbolic links set that will override the PATH changes you make and sure enough it was pointing to the JAVA versions I returned from -version on java and javac. Even if I tried to set a JAVA_HOME as the first entry in path, it still didn't work! I found two solutions here at: JDK 8 and C:\ProgramData\Oracle\Java\javapath · How to switch ... More on stackoverflow.com
🌐 stackoverflow.com
[deleted by user]
What exactly do you want to switch? Do you want to create code for a specific version or switch the default version for windows or use a specific Runtime version for different apps or....? For coding you can set the target version or JDK on project level. Java Apps usually have a config or use the JAVA_HOME variable which points to the wanted Java version/installation. Often there's a start bat where you can set it. More on reddit.com
🌐 r/javahelp
6
1
January 27, 2024
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
🌐
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 - The files I provide for download work in both, PowerShell and Windows command prompt. ... Finally solution that WORKS. Thats also so great that you provided scripts. You just need to remember that after switching java version with script and closing terminal, next terminal will open with java default version, not the one from last script run.
🌐
Damir's Corner
damirscorner.com › blog › posts › 20251226-SwitchingBetweenJdksInWindows.html
Switching between JDKs in Windows | Damir's Corner
December 26, 2025 - Now I can use them to switch to a different JDK in the current session: ➜ 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)
🌐
Medium
gopensource.com › switching-jdk-versions-on-windows-f160e2724e08
Switching JDK versions in Windows | by Abhi Vempati | GopenSource
April 10, 2021 - Here, setx is used to set the variable value (the path) to the respective variable (the name, which in this case is \JAVA_MAIN. The flag -m sets the variable in environmental variables. . Line 4 prints out the JAVA_MAIN value. Lastly, we run the refreshenv.bat (which is discussed later). ... This script (below), switches to the other version of Java, say java10.
Find elsewhere
🌐
GitHub
github.com › orgs › community › discussions › 123161
How to change default Java version in windows-latest? · community · Discussion #123161
May 10, 2024 - Thanks for your comment. I saw actions/setup-java but, at first look, had assumed it is for installing java, not using a version already installed. But after reading this, I now see that it detects a pre-installed version on windows-latest and allows you to switch to it.
🌐
GitHub
gist.github.com › joost-de-vries › 201ad06918077f8a01ae
Windows script for switching java version · GitHub
Windows script for switching java version. GitHub Gist: instantly share code, notes, and snippets.
🌐
Oracle
java.com › en › download › help › path.html
How do I set or change the PATH system variable?
Reopen Command prompt window, and run your java code. To run a different version of Java, either specify the full path, or use the java_home tool:
🌐
Manusobles
manusobles.com › posts › multiple-java-versions-windows
Work with multiple Java JDK versions on Windows | Manu Sobles
May 25, 2021 - @echo off echo "Setting Java 11 as JAVA_HOME as user environment variable" set JAVA_HOME=C:\Program Files\java\jdk-11.0.2 echo Java 11 activated. In my case, I wanted to have it at system level so that my GIT hooks could automatically use the right version. That is it! We can now easily switch between Java versions very easily from any directory:
🌐
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.
🌐
GitHub
gist.github.com › vanjis › d3d93344c10adc684852fce448ca9c1c
Switch java version on windows · GitHub
Switch java version on windows. GitHub Gist: instantly share code, notes, and snippets.
🌐
YouTube
youtube.com › watch
how to switch between the multiple java versions(jdk) | change java version in cmd | sap hybris - YouTube
how to switch between the multiple java versions(jdk) | change java version in cmd | sap hybris | how to switch between multiple java versions | how to switc...
Published   April 1, 2023
🌐
YouTube
youtube.com › watch
How to switch between the multiple Java versions in windows - YouTube
#java How to switch between the multiple Java versions(JDK) in windows 10 | Switch between java 8,11,15,17In this Video i am going to show how to switch or m...
Published   October 28, 2024
🌐
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
Create a new “.bat” file say for example 8_java.bat, replace your Java version path in the below code and use it in the batch file. @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
🌐
YouTube
youtube.com › watch
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
🌐
Reddit
reddit.com › r/javahelp › [deleted by user]
Switch multiple Java versions on Windows without Admin ...
January 27, 2024 - one thing i did on the pc in my ... versions and just delete and unzip the versions there ... You can easily switch Java versions by installing the following VSCode extension....