As a Java programmer, I work on various projects based on different Java versions, especially since the transition to the six-month release cycle. From time to time, I also have a piece of Java code that I want to try out on different Java versions – on the command line without having to click through the menus of my IDE. So it is helpful to be able to quickly and easily change the Java version to be used for compiling code or executing class files.
In this article, I’ll show you how to install several Java versions simultaneously in Windows and how to switch between them on the command line with short commands.
Installation of multiple Java versions
Installing several Java versions at the same time 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:
Attention – you may use the following Oracle distributions only for private purposes and development:
- Java SE 11 / OpenJDK 11
- Java SE 12 / OpenJDK 12
- Java SE 13 / OpenJDK 13
- Java SE 14 / OpenJDK 14
- Java SE 15 / OpenJDK 15
The following version is currently an early access build. You should use it only for testing purposes:
In most cases, 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 unforeseen problems due to 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
Setting environment variables manually
The installers of the Java versions listed above already 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,” and the system properties appear:
Here you click on “Environment Variables…”, after which the following window opens:
As the standard version, I currently use the latest release version, Java 15. Therefore, 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-15\” (please check the version number, you may have a newer one). 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”:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
- Insert the following entry instead:
The path list should then look like this:
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 (more about this below).
Now open a command line to check the settings with the following commands:
As a result, you should see this:
Scripts for changing the Java version
Finally, we come to the possibility to change the Java version quickly and easily. Therefore I create a separate command for each Java version, called, for example,
java12. I put these commands in the directory
C:\Program Files\Java\scripts, which I add to the environment variable “Path” as described before:
The files are named, for example,
java12.bat, and change the environment variables as shown in the following example of
@echo off set JAVA_HOME=C:\Program Files\Java\jdk-15 set Path=%JAVA_HOME%\bin;%Path% echo Java 15 activated.
This script, admittedly kept very simple, inserts the
bin directory of the Java version to be activated at the beginning of the
Path variable, so that is where, for example,
java.exe is looked for first. The path variable becomes longer with each switching. However, since this is only the case for the currently opened command prompt, I don’t think this is a problem.
The following RAR file contains all my scripts from Java 1.2 to Java 16 (you might have to adopt one or the other Java directory to your installation): scripts-up-to-jdk16.zip
You can quickly test whether the scripts work in a command line. Attention: after adding the
scripts directory to the
Path variable, you have to open a new command line.
At this point, the setup is complete. I hope this article was helpful for you, and I’d love to hear your comments. If you liked the article, I’d be happy if you shared it via one of the following share icons.