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 19)

Sven Woltmann
Sven Woltmann
September 20, 2022

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:

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 10 System Properties
Windows 10 System Properties

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

Windows environment variables Java 19
Windows environment variables Java 19

As the default version, I recommend the current release version, Java 19. 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-19". 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-java20-v2.zip

The ZIP file contains scripts named java20.bat, java19.bat, java18.bat, etc., for all Java versions, plus some common scripts starting with "javaX". I suggest you unpack the scripts to C:\Program Files\Java\scripts.

The scripts look like this:

java19.bat:

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

javaX.bat:

@echo off
call javaX-JAVA_HOME %1
set Path=%JAVA_HOME%\bin;%Path%
echo %~1 activated.
Code language: DOS .bat (dos)

javaX-JAVA_HOME.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 19" set JAVA_HOME=C:\Program Files\Java\jdk-19
if %1 == "Java 20" set JAVA_HOME=C:\Program Files\Java\jdk-20Code language: DOS .bat (dos)

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 and enter, e.g., the following commands:

Changing the Java version
Changing the Java version

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

Temporary, Permanent, and System-Wide Java Version Changes

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

That is why there are not one but three scripts for each Java version:

  • java<version>: Activates the Java version in the current command line.
  • java<version>-user: Sets the Java version as the default version for your user account.
  • java<version>-system: Sets the Java version as the default version for the entire system-

The -user variants of the scripts additionally set the JAVA_HOME environment variable with the setx command, permanently writing the change to the registry:

java19-user.bat:

@echo off
call javaX-user "Java 19"Code language: DOS .bat (dos)

javaX-user.bat:

@echo off
call javaX-JAVA_HOME %1
setx JAVA_HOME "%JAVA_HOME%"
set Path=%JAVA_HOME%\bin;%Path%
echo %~1 activated as user default.
Code language: DOS .bat (dos)

The -system variants also specify the /M parameter in the setx command. This sets the system-wide environment variable instead of the user-specific one:

java19-system.bat:

@echo off
call javaX-system "Java 19"Code language: DOS .bat (dos)

javaX-system.bat:

@echo off
call javaX-JAVA_HOME %1
setx JAVA_HOME "%JAVA_HOME%" /M
set Path=%JAVA_HOME%\bin;%Path%
echo %~1 activated as system-wide default.
Code language: DOS .bat (dos)

Attention: To set the system-wide Java version, you must open the command line 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.