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.
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.
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.
C:\jdk1.3.1_28\by default – I recommend changing this to
C:\Program Files (x86)\Java\jdk1.3.1_28\.
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:
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
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 16.0.2. Accordingly, you should make the following settings:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
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.
Now open a command line to check the settings with the following commands:
Here's what you should see:
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-java17ea.zip
The ZIP file contains scripts named
java15.bat, etc., for all Java versions. I suggest you unpack the scripts to
The scripts look like this (
@echo off set JAVA_HOME=C:\Program Files\Java\jdk-16.0.2 set Path=%JAVA_HOME%\bin;%Path% echo Java 16 activated.
The script updates the
JAVA_HOME environment variable and inserts 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.)
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 batch file corresponds to the installation path of the Java version you want to activate.
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 16, 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 set JAVA_HOME=C:\Program Files\Java\jdk-16.0.2 setx JAVA_HOME "%JAVA_HOME%" set Path=%JAVA_HOME%\bin;%Path% echo Java 16 activated as user default.
-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 set JAVA_HOME=C:\Program Files\Java\jdk-16.0.2 setx JAVA_HOME "%JAVA_HOME%" /M set Path=%JAVA_HOME%\bin;%Path% echo Java 16 activated as system-wide default.
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.
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.