How to switch Java versions in Windows

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. 


Environment variables

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 java and 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 eclipse.ini file).


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 16. 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-16\" (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:\ProgramData\Oracle\Java\javapath
    • C:\Program Files (x86)\Common Files\Oracle\Java\javapath
  • Insert the following entry instead:
    • %JAVA_HOME%\bin

The path list should then look like this:

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 (more about this below).

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

  • echo %JAVA_HOME%
  • java -version

As a result, you should see this:


Source: happycoders.eu