Windows switch java version

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 and in PowerShell:

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:

  • 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)
  • Java SE 20 / OpenJDK 20 (→ The most important new features in Java 20)
  • Java SE 21 / OpenJDK 21 (→ The most important new features in Java 21)
  • Java SE 22 / OpenJDK 22 (→ The most important new features in Java 22)
  • Java SE 23 / OpenJDK 23 (→ The most important new features in Java 23)

The following version is currently an early access build. You should use it only for testing purposes:

  • JDK 24 Early-Access Build

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

At this point, you can press «Enter» to open the system properties:

Windows System Properties

Click on «Environment Variables…» and the following window opens:

Windows environment variables Java 23

As the default version, I recommend the current release version, Java 23. 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-23». 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

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»

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-java24.zip

The ZIP file contains scripts named

  • java23.bat, java22.bat, java21.bat, etc., for all Java versions,
  • the corresponding files java23.ps1, java22.ps1, etc. for PowerShell,
  • plus two common scripts javaX.bat and javaX.ps1.

I suggest you unpack the scripts to C:\Program Files\Java\scripts.

The scripts look like this:

java23.bat:

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

java23.ps1

javaX "Java 23" $args[0]
Code language: PowerShell (powershell)

javaX.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 21" set JAVA_HOME=C:\Program Files\Java\jdk-21
if %1 == "Java 22" set JAVA_HOME=C:\Program Files\Java\jdk-22
if %1 == "Java 23" set JAVA_HOME=C:\Program Files\Java\jdk-23

if "%~2" == "perm" (
  setx JAVA_HOME "%JAVA_HOME%" /M
)

set Path=%JAVA_HOME%\bin;%Path%

echo %~1 activated.Code language: DOS .bat (dos)

javaX.ps1:

param ($javaVersion, $perm)

switch ($javaVersion) {
  "Java 1.2" { $env:JAVA_HOME = "C:\Program Files (x86)\Java\jdk1.2.2" }
  "Java 1.3" { $env:JAVA_HOME = "C:\Program Files (x86)\Java\jdk1.3.1_28" }
  ...
  "Java 21" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-21" }
  "Java 22" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-22" }
  "Java 23" { $env:JAVA_HOME = "C:\Program Files\Java\jdk-23" }
}

if ($perm -eq "perm") {
  [Environment]::SetEnvironmentVariable("JAVA_HOME", $env:JAVA_HOME, [System.EnvironmentVariableTarget]::Machine)
}

$env:Path = $env:JAVA_HOME + '\bin;' + $env:Path

Write-Output "$javaVersion activated."Code language: PowerShell (powershell)

In the files javaX.bat and javaX.ps1, you probably have to adjust some paths to the installed Java versions.

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

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 or PowerShell and enter, for instance, the following commands:

Changing the Java version in PowerShell

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

Temporary and Permanent Java Version Changes

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

If you want to change the Java version permanently, just add the parameter «perm» to the corresponding command, e.g.

java23 perm

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

Understanding Environment Variables in Windows

When working with Java or any programming language on a Windows Operating System, environment variables play a crucial role in setting the path to the version of your software that you wish to use. By defining these environment variables, you are essentially telling your system where to find the necessary executables for any given program.

Common Java Environment Variables

There are two key types of environment variables when working with Java:

  1. PATH — This variable tells your system where to look for executable files when a command is entered.
  2. JAVA_HOME — This points specifically to the directory where Java is installed.

By correctly setting these variables, your system will know to utilize the specific Java version you intend to work with.

Switching Java Versions

Initial Setup

  1. Install the Desired JDK Version: Make sure Java 17 (or whichever version you need) is installed on your machine. You can download it from the official Oracle or OpenJDK site.

  2. Update Environment Variables for Java:

    • Open the Environment Variables window:
      1. Press Win + Pause/Break.
      2. Click on Advanced system settings.
      3. Click on Environment Variables…
    • Modify JAVA_HOME:
      • Set JAVA_HOME to the JDK 17 directory (e.g., C:\Program Files\Java\jdk-17).
    • Update PATH Variable:
      • Find PATH in the System variables section and click Edit.
      • Add a new entry for the bin directory of your JDK 17 installation (e.g., C:\Program Files\Java\jdk-17\bin). Ensure this new path is listed before any other Java versions in the PATH list.

Applying Changes to Current Sessions

When you update your environment variables in Windows, changes typically apply to new instances of the command line and not to currently open instances. Therefore, if you want your cmd window to acknowledge the new Java version, you’ll need to open a new command prompt session. Simply closing and reopening the existing one will suffice.

Verifying the Java Version

To ensure the correct version of Java is now being used, open a new cmd window and type:

This command will output the version of Java that your system is currently using. If your changes were applied correctly, you should see something along the lines of:

java version "17.x.x"
Java(TM) SE Runtime Environment (build 17.x.x+xx)
Java HotSpot(TM) 64-Bit Server VM (build 17.x.x+xx, mixed mode)

Conclusion

Switching between different Java versions on Windows systems can be managed effectively with the proper setting of environment variables such as JAVA_HOME and modifications of the PATH. Once these settings are correctly adjusted, restarting the command prompt or any application that requires Java will ensure the new version is being utilized. Notably, this does not require a system reboot, saving both time and effort.

Ensuring your system is employing the correct Java version can prevent compile errors and compatibility issues within your development processes. Make sure to follow the steps accurately, and ensure that any command prompt reopening reflects the changes made to environment variables.

Switching Java versions on Windows 10 is a straightforward process that can be accomplished in just a few steps. You’ll need to adjust your system’s environment variables to point to the version of Java you want to use. This involves accessing the Control Panel, navigating to the System settings, and then modifying the Java location path. Once completed, your computer will run the specified version of Java for all your applications and projects.

Switching Java versions on Windows 10 allows you to develop and run applications using the specific version of Java you need. Follow these steps to ensure your system uses the desired Java version.

Step 1: Download and Install the Desired Java Version

First, download and install the Java version you want from the official Oracle website or another reputable source.

Ensure that you complete the installation process and take note of the installation directory, as you’ll need this path for the following steps.

Step 2: Open System Properties

Next, access the System Properties by right-clicking on ‘This PC’ on your desktop or in File Explorer, then selecting ‘Properties.’

This will take you to a screen where you can view and adjust various system settings.

Step 3: Access Environment Variables

Click on ‘Advanced system settings’ on the left, and then click ‘Environment Variables’ at the bottom of the System Properties window.

This section allows you to change various environment settings, including the Java version your system uses.

Step 4: Update the JAVA_HOME Variable

In the Environment Variables window, find the JAVA_HOME variable in the ‘System variables’ section and click ‘Edit.’

Set the variable value to the path of the installed Java version from Step 1. If JAVA_HOME doesn’t exist, create a new variable with this name.

Step 5: Update the Path Variable

Lastly, in the same Environment Variables window, locate the ‘Path’ variable, select it, and click ‘Edit.’

Add the path to the bin directory of your Java installation. This ensures all Java commands use the correct version.

After completing these steps, your Windows 10 system will be using the Java version you specified. You can verify this by opening Command Prompt and typing java -version to see the active version.

Tips for Switching Java on Windows 10

  • Always download Java from a trusted source to avoid security risks.
  • Double-check the Java version installed by verifying with the java -version command.
  • If you don’t see JAVA_HOME in the Environment Variables, you can create it.
  • Remember that some applications might require specific Java versions, so switch accordingly.
  • Keep multiple versions installed if working on varied projects but manage them carefully to avoid confusion.

Frequently Asked Questions

What happens if I don’t update the Path variable?

If you skip updating the Path variable, your system might not recognize the correct Java version, leading to errors when running Java applications.

Can I have multiple Java versions installed on Windows 10?

Yes, you can have multiple Java versions installed. Use the JAVA_HOME variable to switch between them as needed.

Why does my application not recognize the new Java version?

Ensure that both JAVA_HOME and the Path variable are correctly set. Also, restart your application or system if necessary.

How do I know which Java version is currently active?

Open the Command Prompt and type java -version to display the active Java version.

Is it necessary to restart my computer after switching Java versions?

Usually, it’s not required, but if you face issues, a restart might help your system recognize the changes.

Summary

  1. Download and install the desired Java version.
  2. Open System Properties.
  3. Access Environment Variables.
  4. Update the JAVA_HOME variable.
  5. Update the Path variable.

Conclusion

Switching Java versions on Windows 10 is a critical skill for developers working with different projects that may require specific Java versions. Ensuring that you have the right version can lead to smoother development processes and fewer errors. Remember, the key steps involve downloading the desired version, updating the JAVA_HOME environment variable, and adjusting the Path variable.

When you’re switching Java versions, think of it as changing gears in a car. Each project might need a different gear to run smoothly, and adjusting Java is like shifting to just the right gear. It’s a straightforward process once you get the hang of it, and it keeps your software running efficiently.

Stay updated with the latest Java versions and security patches to maintain an optimal development environment. If you find yourself switching frequently, consider tools like SDKMAN! to manage multiple Java installations more easily. Happy coding!

Matt Jacobs has been working as an IT consultant for small businesses since receiving his Master’s degree in 2003. While he still does some consulting work, his primary focus now is on creating technology support content for SupportYourTech.com.

His work can be found on many websites and focuses on topics such as Microsoft Office, Apple devices, Android devices, Photoshop, and more.

Java is a widely-used programming language that runs on a variety of platforms. Developers often need to switch between different Java versions for various projects, and with Windows 11, the ability to manage these versions has become more streamlined. Whether you’re a hobbyist, a student, or a professional developer, knowing how to change your Java version can save you from compatibility headaches.

In this guide, we will detail the steps necessary to change Java versions in Windows 11. We will cover the process of checking the installed Java versions, installing a new version, configuring environmental variables, and ensuring the correct version is in use.

Understanding Java Versions

Before diving into the technical steps, it’s essential to understand what Java versions are and why you might want to switch between them. Java has different versions, including:

  1. Java Development Kit (JDK): This is what developers need to create Java applications.
  2. Java Runtime Environment (JRE): This is necessary for running Java applications.
  3. Java SE (Standard Edition): Contains the core libraries and tools.

The Java ecosystem evolves continuously, with new features, performance improvements, and security updates. Different projects may require specific Java versions, so you might find yourself needing to switch versions frequently.

Checking Installed Java Versions

To change the Java version on your Windows 11 system, the first step is to see what versions you currently have installed.

  1. Open Command Prompt: Press Win + R, type cmd, and hit Enter.

  2. Check Installed Versions: In the Command Prompt, type:

    java -version

    This command will display the currently active Java version on your system.

  3. Check for JDK: You may also want to check for the installed JDK version by typing:

    javac -version

    Ensure both JAVA and JAVAC outputs are correct, as these will influence your development environment.

If you have multiple versions installed but are unsure of their installation paths, you can look for them in the Program Files directory, usually found under C:Program FilesJava.

Installing a New Java Version

If the required Java version is not installed on your system, you can download and install it easily.

  1. Download JDK: Go to the official Oracle website or OpenJDK site, choose the version you need, and download it. For example:

    • Oracle JDK: Oracle Java Downloads
    • OpenJDK: OpenJDK
  2. Run the Installer: Double-click the downloaded .exe file and follow the installation wizard to complete the installation. Make sure to note the installation directory, as you will need this later.

  3. Verify the Installation: After installation, you can verify that the new version is installed successfully by running java -version again in Command Prompt.

Configuring Environmental Variables

Changing the Java version involves updating the environmental variables in Windows. This will allow you to specify which version of Java should be used system-wide.

  1. Open System Properties:

    • Press Win + R to open the Run dialog.
    • Type sysdm.cpl and press Enter. This will open the System Properties window.
  2. Go to Environment Variables:

    • Click on the Advanced tab.
    • At the bottom, click on Environment Variables.
  3. Find JAVA_HOME:

    • In the Environment Variables window, under System variables (the bottom section), check if there is a variable named JAVA_HOME.
    • If it exists, click on it and then click Edit. If it doesn’t exist, click on New... to create it.
  4. Set JAVA_HOME:

    • Set the variable name as JAVA_HOME.
    • For the variable value, enter the path of the JDK installation directory (e.g., C:Program FilesJavajdk-17).
  5. Updating the Path Variable:

    • Now, you need to update the Path variable.
    • In the same Environment Variables window, find the Path variable in the System variables section and click Edit.
    • In the Edit Environment Variable window, click New and add the path to the JDK’s bin folder (e.g., C:Program FilesJavajdk-17bin).
    • Click OK to close all dialog boxes.

Validating the Java Version Change

After changing the environment variables, you will want to verify that your system recognizes the new Java version.

  1. Reopen Command Prompt: Close any open Command Prompt windows and open a new one, as changes will not reflect in already opened terminals.

  2. Check Java Version: Again, type:

    java -version

    This should now display the version you installed and configured in your environment variables.

  3. Test Java Compiler: You can also check if the new version of javac is active by running:

    javac -version

    Both outputs should match the version you installed.

Switching Between Multiple Java Versions

If you have multiple versions of Java on your machine, you can switch between them using the same method outlined above. However, it’s more practical to set the environment variables for temporary use via command line.

Temporarily Changing the Java Version

You can temporarily change the Java version for the duration of your command prompt session without modifying the system environment variables. Here’s how:

  1. Open Command Prompt.
  2. Set JAVA_HOME locally:
    set JAVA_HOME=C:Program FilesJavajdk-11
  3. Add to Path:
    set PATH=%JAVA_HOME%bin;%PATH%

Now, if you check java -version, it should reflect the version of JDK 11 for this session.

Using Java Version Managers

For those frequently switching between Java versions, consider using a Java Version Manager. A popular tool is SDKMAN!, which allows you to manage multiple versions with ease.

  1. Installing SDKMAN!:

    • Open a terminal and use the following command to install it:
      curl -s "https://get.sdkman.io" | bash
  2. Follow the Instructions: Follow the on-screen instructions to complete the installation.

  3. Install Java Versions: After installation, use SDKMAN! to install specific versions:

    sdk install java 11.0.11-open
    sdk install java 17.0.0-open
  4. Switch Versions: You can switch versions easily:

    sdk use java 11.0.11-open

This method provides a lot of flexibility without the need for manual updates to environment variables.

Troubleshooting Common Issues

Switching Java versions can sometimes lead to various issues. Here are some common problems and how to solve them:

  • JAVA_HOME Not Set Properly: Ensure that the JAVA_HOME path points to the correct JDK installation directory. Recheck any typos or incorrect paths.

  • Command Not Recognized: If you receive an error that java or javac is not recognized, it’s likely due to the PATH variable not being set correctly. Double-check the bin folder path.

  • Version Conflicts: If you switch versions and something still doesn’t work, ensure that there are no leftover cache or configurations that could cause conflicts. Deleting old versions that are no longer needed can help.

  • Reboot Your System: If everything appears set up correctly but you’re still encountering issues, a simple restart of your system may solve environmental variable refresh problems.

Conclusion

Changing the Java version in Windows 11 isn’t overly complicated, but it requires careful attention to detail, especially when managing the environment variables. By following the steps listed above, you can ensure a smooth transition between various Java versions, whether for development or runtime requirements. Tools like SDKMAN! can further simplify the management process, allowing you to focus on development rather than configuration.

As Java continues to evolve, staying agile with version management is essential for developers. Take the time to familiarize yourself with the procedures and tools available, and you’ll find that switching Java versions becomes a straightforward task that augments your development workflow. Happy coding!

windows-java-version-switcher

This is a small windows tool to switch between different Java Versions.

Setup

Java

In order to switch between Java Versions you need multiple Java Versions installed and get their path.

I’ve installed my Java Versions with Chocolatey.
After that you can install different Java Versions with the following command (admin cmd):

choco install zulu8 -y
choco install zulu11 -y
choco install zulu17 -y

You might want to add an Auto-Updater:

choco upgrade all -y
choco install choco-upgrade-all-at-startup -y

After that you can find the Java Versions Paths with the following CMD command:

Output:

where-java.png

Windows Path Variables

JAVA_HOME

In order to switch the Version we need to add JAVA_HOME to the System Path Variables.

path-variables.png

To get there go into your Windows taskbar and select «System» and then «Advanced System Settings» and then «Environment Variables».
Here you can add a new System Variable with the name «JAVA_HOME» and one of the Java Paths from before.

Custom Scripts

Now please download this repository and extract it to a folder of your choice.
Then go into your user variables and add the path to the folder where you extracted the repository to the Path Variable.

If you did not install your Java Versions with Chocolatey you might need to change the paths for the scripts in the folder «scripts» to your needs.

Test

To test please execute the following commands in your CMD:

where java
java11
java --version
java17
java --version

This should result in:

switcher.png

java8 does not support «—version» so do not be surprised if you get an error.

Hope that helps :)

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Windows 7 logon screen changer
  • Как перевернуть рабочий стол на windows
  • Сделать загрузочный диск windows на mac os
  • Epson perfection v33 photo драйвер для windows 10
  • Администратор заблокировал это приложение в windows 10 как исправить