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 - In the terminal I use the commands in ("java8"/"java16"), it confirms that my version has been switched, but as soon as I close and open a new terminal, it is still the version I tried to switch off of. I have very little experience with this stuff, and any help would be greatly appreciated. ... Hi Chase, you can use the recently added commands "java8-user" and "java16-user" (for example) to set the Java version permanently for your user account (see the newly added section "Temporary, Permanent and System-Wide Java Version Changes").
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
Unable to change Java Version in windows - Stack Overflow
Previously I am using Java 1.8 in my machine. But now i need to use Java 1.6. So I changed the below values in system environment variables. JAVA_HOME U:\POC\jdk1.6.0_31 PATH U:\POC\jdk1.6.0_31... More on stackoverflow.com
🌐 stackoverflow.com
java - Switching between different JDK versions in Windows - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
How do I change default version of java from my terminal?
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
11
2
December 22, 2023
🌐
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 - @echo off set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_271 set Path=%JAVA_HOME%\bin;%Path% echo Java 8 activated. This batch requires me to also modify the enviromental variables.
🌐
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 - Now you need to write code into these files. Fo r Example: echo off set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202 set PATH=%JAVA_HOME%\bin;%PATH% echo Java Version java -version
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.

Find elsewhere
🌐
Damir's Corner
damirscorner.com › blog › posts › 20251226-SwitchingBetweenJdksInWindows.html
Switching between JDKs in Windows | Damir's Corner
December 26, 2025 - I decided to use JDK 25 by default, so I made the following changes to my user environment variables: I set JAVA_HOME to the JDK path: C:\Users\damir\.jdks\openjdk-25.0.1. I added the bin subfolder inside it to PATH: C:\Users\damir\.jdks\openjdk-25.0.1\bin. This was enough to get Java 25 working in a newly opened terminal: ➜ java -version openjdk version "25.0.1" 2025-10-21 OpenJDK Runtime Environment (build 25.0.1+8-27) OpenJDK 64-Bit Server VM (build 25.0.1+8-27, mixed mode, sharing)
🌐
Reddit
reddit.com › r/javahelp › how do i change default version of java from my terminal?
r/javahelp on Reddit: How do I change default version of java from my terminal?
December 22, 2023 -

Currently, when I input "java -version", I get an output of "20.0.1". When I input "/usr/libexec/java_home -V", It says I have 20.0.1, 17.0.9, and 11.0.9.
When I input "export JAVA_HOME=$(/usr/libexec/java_home -v 17.0.9);" to change the version and run "java -version" again, It shows as 17.0.9.
The problem is when I close my terminal and check my java version, it returns back to version 20. How do I permanently change my default java version. Thanks.

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
🌐
GitHub
gist.github.com › rwunsch › d157d5fe09e9f7cdc858cec58c8462d6
Windows - Change Java version (JDK) per command line via batch file · GitHub
Save rwunsch/d157d5fe09e9f7cdc858cec58c8462d6 to your computer and use it in GitHub Desktop. Download ZIP · Windows - Change Java version (JDK) per command line via batch file · Raw · jdk.bat · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
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 - Now we've come to the last terminal I'll config for this blog, The powerful Cmder · 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
🌐
MTU Service Desk
servicedesk.mtu.edu › TDClient › 1801 › Portal › KB › ArticleDet
Article - Changing Java versions (Linux)
In order to activate a Java version, run the script corresponding to the version you would like to work with, then reload or resource your terminal: For example the following command activates OpenJDK Java 8: `source /opt/java-selection/ope...
🌐
Super User
superuser.com › questions › 1419753 › how-to-change-java-jdk-version
How to change java jdk version? - Super User
I want to install the new 11.0.02 java jdk. I tried to use this link: first source and afterwards this: second source Now, using cmd, I run these two commands: java -version ==> the older versio...
🌐
Baeldung
baeldung.com › home › installation › switch between multiple java versions
Switch Between Multiple Java Versions | Baeldung on Linux
March 18, 2024 - Let’s see how to use galternatives to choose a Java version. First, we start galternatives from either the terminal or the application menu: ... Under the Groups column in the main window, we can see a list of programs. We can look for or add java in the list. For instance, on our system, we see two Java versions: Notably, OpenJDK 8 is the default version now. Let’s change the Java version to OpenJDK 11 by using the radio buttons:
🌐
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 - # Function to switch JDK versions function setjdk() { echo "Select the java version you wish to switch to from the following:" echo options=() count=-1 while IFS= read -r line do if [[ ! "$line" == M* ]] && [[ ! "$line" == /* ]] && [[ ! -z "$line" ]]; then options+=("$line") ((count++)) echo '['"$count"']'${options["$count"]} fi done < <(/usr/libexec/java_home -V 2>&1) echo read -p "Please chose a value from the options above: " selectedOption </dev/tty if [ "$count" -ge "$selectedOption" ] && [ "$selectedOption" -ge '0' ]; then removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/
Top answer
1 of 16
2562

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

or you can specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now when you run java -version you will see:

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Add the export JAVA_HOME… line to your shell’s init file.

For Bash (as stated by antonyh):

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

For Fish (as stated by ormurin)

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

Updating the .zshrc file should work:

nano ~/.zshrc

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)

Press CTRL+X to exit the editor Press Y to save your changes

source ~/.zshrc
echo $JAVA_HOME
java -version
2 of 16
608

This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).

As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

Solution without 3rd party tools:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

System launcher will use the JDK with highest version among those that have an Info.plist file.

When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).


Details of investigation in this gist.


(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.

🌐
MTU Service Desk
servicedesk.mtu.edu › TDClient › KB › ArticleDet
Changing Java versions (Linux) - Service Desk
In order to activate a Java version, run the script corresponding to the version you would like to work with, then reload or resource your terminal: For example the following command activates OpenJDK Java 8: `source /opt/java-selection/ope...