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:
To enable these Java version change commands on your system as well, follow this step-by-step guide.
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.
- Java SE 1.1 – You can no longer install this version on 64-bit Windows.
- Java SE 1.2 – Installed to
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:
- Java SE 5
- Java SE 6
- Java SE 7
- Java SE 8
- Java SE 9 / OpenJDK 9
- Java SE 10 / OpenJDK 10 (→ The most important new features in Java 10)
Attention – you may use the following Oracle distributions only for private purposes and development:
- Java SE 11 / OpenJDK 11 (→ The most important new features in Java 11)
- Java SE 12 / OpenJDK 12 (→ The most important new features in Java 12)
- Java SE 13 / OpenJDK 13 (→ The most important new features in Java 13)
- Java SE 14 / OpenJDK 14 (→ The most important new features in Java 14)
- Java SE 15 / OpenJDK 15 (→ The most important new features in Java 15)
- Java SE 16 / OpenJDK 16 (→ The most important new features in Java 16)
- Java SE 17 / OpenJDK 17 (→ The most important new features in Java 17)
- Java SE 18 / OpenJDK 18 (→ The most important new features in Java 18)
- Java SE 19 / OpenJDK 19 (→ The most important new features in Java 19)
The following version is currently an early access build. You should use it only for testing purposes:
- JDK 20 Early-Access Build (→ The most important new features in Java 20)
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
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
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:
At this point, you can press "Enter" to open the system properties:
Click on "Environment Variables…" and the following window opens:
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:\Program Files (x86)\Common Files\Oracle\Java\javapath
- Insert the following entry instead:
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):
The last entry ensures that
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:
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
java18.bat, etc., for all Java versions, plus some common scripts starting with "javaX". I suggest you unpack the scripts to
The scripts look like this:
@echo off call javaX "Java 19"Code language: DOS .bat (dos)
@echo off call javaX-JAVA_HOME %1 set Path=%JAVA_HOME%\bin;%Path% echo %~1 activated.Code language: DOS .bat (dos)
@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
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):
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:
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-
-user variants of the scripts additionally set the
JAVA_HOME environment variable with the
setx command, permanently writing the change to the registry:
@echo off call javaX-user "Java 19"Code language: DOS .bat (dos)
@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)
-system variants also specify the
/M parameter in the
setx command. This sets the system-wide environment variable instead of the user-specific one:
@echo off call javaX-system "Java 19"Code language: DOS .bat (dos)
@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.