how to switch java versions in windowshow to switch java versions in windows
HappyCoders Glasses

How to Change Java Versions in Windows
(updated for Java 23)

Sven Woltmann
Sven Woltmann
Last update: November 26, 2024

In this article, I will show you how to install multiple Java versions on Windows and how to change the Java version on the command line and in PowerShell:

Multiple Java versions on Windows

To enable these Java version change commands on your system as well, follow this step-by-step guide.

Let's go…

Step 1: Installing Multiple Java Versions

Installing multiple Java versions in parallel is incredibly easy in Windows. You can download and run the installer for each version, which automatically installs the versions in separate directories.

Download Sources

  • Java SE 1.1 – You can no longer install this version on 64-bit Windows.
  • Java SE 1.2 – Installed to C:\jdk1.2.2\ and C:\Program Files (x86)\JavaSoft\JRE\1.2\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.2.2\ and C:\Program Files (x86)\Java\jre1.2.2\ for the sake of clarity.
  • Java SE 1.3 – Installed to C:\jdk1.3.1_28\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.3.1_28\.
  • Java SE 1.4 – Installed to C:\j2sdk1.4.2_19\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.4.2_19\.

Starting with the following versions, you don't need to change the default installation directories:

Attention – you may use the following Oracle distributions only for private purposes and development:

The following version is currently an early access build. You should use it only for testing purposes:

Step 2: Define Java Environment Variables

The following two environment variables decide which Java version an application uses:

  • JAVA_HOME – many start scripts use this variable.
  • Path – is used when running a Java binary (such as java and javac) from the console.

These variables should always point to the same Java installation to avoid inconsistencies. Some programs, such as Eclipse, define the Java version in a separate configuration file (for Eclipse, for example, this is the entry "-vm" in the eclipse.ini file).

Manually Setting the Java Environment Variables

The Java installers create various environment variables, which you need to clean up first (see below). The fastest way to change the environment variables is to press the Windows key and type "env" – Windows then offers "Edit the system environment variables" as a search result:

Opening Windows environment variables
Opening Windows environment variables

At this point, you can press "Enter" to open the system properties:

Windows System Properties
Windows System Properties

Click on "Environment Variables…" and the following window opens:

Windows environment variables Java 23
Windows environment variables Java 23

As the default version, I recommend the current release version, Java 23. Accordingly, you should make the following settings:

  • The top list ("User variables") should not contain any Java-related entries.
  • The lower list ("System variables") should contain an entry "JAVA_HOME = C:\Program Files\Java\jdk-23". If this entry does not exist, you can add it with "New…". If it exists but points to another directory, you can change it with "Edit…".
  • Delete the following entries under "Path" (if they exist):
    • C:\ProgramData\Oracle\Java\javapath
    • C:\Program Files (x86)\Common Files\Oracle\Java\javapath
  • Insert the following entry instead:
    • %JAVA_HOME%\bin

The entry should then look like the following (the other entries in the list will probably look different for you since you have other applications installed than I do):

Adding "%JAVA_HOME%\bin" to the "Path" system variable
Adding "%JAVA_HOME%\bin" to the "Path" system variable

The last entry ensures that Path and JAVA_HOME are automatically consistent.

Attention: this only works for the default setting configured here. If you change JAVA_HOME via the command line, you have to adjust Path accordingly. But don't worry – the scripts you can download in the next step will do that automatically.

How to Check Your Java Version on Windows

Now open a command line to check the settings with the following commands:

echo %JAVA_HOME%
java -versionCode language: plaintext (plaintext)

Here's what you should see:

Check your Java version with "cmd"
Check your Java version with "cmd"

Step 3: Install the Scripts to Change the Java Version

To change the Java version on the command line, I have prepared some batch files that you can copy to your system. Here is the link: scripts-up-to-java24.zip

The ZIP file contains scripts named

  • java23.bat, java22.bat, java21.bat, etc., for all Java versions,
  • the corresponding files java23.ps1, java22.ps1, etc. for PowerShell,
  • plus two common scripts javaX.bat and javaX.ps1.

I suggest you unpack the scripts to C:\Program Files\Java\scripts.

The scripts look like this:

java23.bat:

@echo off
call javaX "Java 23" %1Code language: DOS .bat (dos)

java23.ps1

javaX "Java 23" $args[0]
Code language: PowerShell (powershell)

javaX.bat:

@echo off

if %1 == "Java 1.2" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.2.2
if %1 == "Java 1.3" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.3.1_28
...
if %1 == "Java 21" set JAVA_HOME=C:\Program Files\Java\jdk-21
if %1 == "Java 22" set JAVA_HOME=C:\Program Files\Java\jdk-22
if %1 == "Java 23" set JAVA_HOME=C:\Program Files\Java\jdk-23

if "%~2" == "perm" (
  setx JAVA_HOME "%JAVA_HOME%" /M
)

set Path=%JAVA_HOME%\bin;%Path%

echo %~1 activated.Code language: DOS .bat (dos)

javaX.ps1:

param ($javaVersion, $perm)

switch ($javaVersion) {
  "Java 1.2" { $env:JAVA_HOME = "C:\Program Files (x86)\Java\jdk1.2.2" }
  "Java 1.3" { $env:JAVA_HOME = "C:\Program Files (x86)\Java\jdk1.3.1_28" }
  ...
  "Java 21" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-21" }
  "Java 22" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-22" }
  "Java 23" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-23" }
}

if ($perm -eq "perm") {
  [Environment]::SetEnvironmentVariable("JAVA_HOME", $env:JAVA_HOME, [System.EnvironmentVariableTarget]::Machine)
}

$env:Path = $env:JAVA_HOME + '\bin;' + $env:Path

Write-Output "$javaVersion activated."Code language: PowerShell (powershell)

In the files javaX.bat and javaX.ps1, you probably have to adjust some paths to the installed Java versions.

The scripts update the JAVA_HOME environment variable and insert the bin directory at the beginning of the Path variable. That makes it the first directory to be searched for the corresponding executable when you run Java commands such as java or javac.

(The Path variable gets longer with each change. Do not worry about it. This only affects the currently opened command line.)

Step 4: Add the Script Directory to the Path

To be able to call the scripts from anywhere, you have to add the directory to the "Path" environment variable (just like you did with "%JAVA_HOME%\bin" in the second step):

Adding "C:\Program Files\Java\scripts" to the "Path" system variable
Adding "C:\Program Files\Java\scripts" to the "Path" system variable

If you have installed the latest releases of all Java versions, you can use the scripts without any further adjustments. Open a new command line or PowerShell and enter, for instance, the following commands:

Changing the Java version in PowerShell
Changing the Java version in PowerShell

If one of the commands does not activate the expected Java version, please check if the path in the javaX.bat and javaX.ps1 files corresponds to the installation path of the Java version you want to activate.

Temporary and Permanent Java Version Changes

The commands presented up to this point only affect the currently opened command line or PowerShell. As soon as you open another command line, the default version defined in step 2 is active again (Java 23, if you have not changed anything).

If you want to change the Java version permanently, just add the parameter "perm" to the corresponding command, e.g.

java23 perm

Attention: To set the Java version permanently, you must open the command line or PowerShell as an administrator. Otherwise, you will get the error message "ERROR: Access to the registry path is denied.

What You Should Do Next…

I hope you were able to follow the instructions well and that the commands work for you.

Now I would like to hear from you:

Were you able to follow the steps well – or do you have unanswered questions?

Either way, let me know by leaving a comment below.