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\
andC:\Program Files (x86)\JavaSoft\JRE\1.2\
by default – I recommend changing this toC:\Program Files (x86)\Java\jdk1.2.2\
andC:\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 toC:\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 toC:\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 asjava
andjavac
) 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:
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 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):
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 -version
Code 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-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
andjavaX.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" %1
Code 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):
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:
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.
Changing the Java version on Windows 10 Command Prompt can be a useful skill for Java developers and enthusiasts. Whether you need to switch to a different version for compatibility reasons or simply want to experiment with different Java versions, knowing how to change the Java version on Command Prompt can come in handy. This blog post will guide you through the process, providing detailed instructions and insights to help you successfully change Java versions on Windows 10.
Video Tutorial:
The Challenge of Changing Java Version on Windows 10 Command Prompt
Many new Java developers may struggle with changing the Java version on Command Prompt for the first time. The Command Prompt is a powerful tool that allows users to execute commands and interact with their computer’s operating system. However, understanding how to navigate and manipulate the Command Prompt to switch Java versions can be confusing.
Things You Should Prepare for
Before you begin changing the Java version on Windows 10 Command Prompt, there are a few things you should prepare:
1. Ensure that you have multiple Java versions installed on your Windows 10 computer. If you have only one version of Java installed, you may need to download and install additional versions from the official Java website.
2. Familiarize yourself with the Command Prompt interface. Understanding basic Command Prompt commands and navigation will make the process of changing Java versions easier.
3. Make note of the location of each Java installation on your computer. You will need this information when specifying the Java version in Command Prompt commands.
Now that you are prepared, let’s explore four different methods to change the Java version on Windows 10 Command Prompt.
Method 1. Changing Java Version Using “java” Command
To change the Java version using the «java» command, follow these steps:
1. Open Command Prompt by pressing the Windows key + R, typing «cmd,» and hitting Enter.
2. Type «java -version» in the Command Prompt and press Enter to check the currently installed Java version.
3. Locate the desired Java version’s installation directory on your computer.
4. In Command Prompt, type the following command to change the Java version:
«`
set PATH=«C:\path\to\desired\java\version\bin»;%PATH%
«`
Replace «C:\path\to\desired\java\version\bin» with the actual path to the desired Java version’s installation directory.
5. Now, type «java -version» again to verify that the Java version has been successfully changed.
Pros:
1. Quick and straightforward method
2. Does not require modifying environment variables
3. Can be easily reversed by closing the Command Prompt window
Cons:
1. Only temporary change for the current Command Prompt session
2. Not suitable for permanently setting the Java version
Method 2. Changing Java Version Using Environment Variables
To change the Java version using environment variables, follow these steps:
1. Right-click the Start button and select System.
2. In the System window, click on «Advanced system settings» on the left.
3. In the System Properties window, click on the «Environment Variables» button.
4. In the Environment Variables window, under «System variables,» locate the «Path» variable and click on «Edit.«
5. In the Edit Environment Variable window, click on «New» and enter the path to the desired Java version’s installation directory.
6. Click «OK» to save the changes.
7. Now, open Command Prompt and type «java -version» to verify that the Java version has been successfully changed.
Pros:
1. Allows for permanently setting the Java version
2. Changes apply to all Command Prompt sessions on the computer
3. Can easily switch between different Java versions by modifying the «Path» variable
Cons:
1. Requires modifying system environment variables, which may be intimidating for some users
2. Changing the «Path» variable incorrectly can cause system-wide issues
Method 3. Changing Java Version Using jEnv
To change the Java version using jEnv, follow these steps:
1. Download and install jEnv from the official jEnv GitHub repository.
2. Open Command Prompt and navigate to the directory where you installed jEnv.
3. In Command Prompt, type the following command to enable jEnv:
«`
jenv enable-plugin exports
«`
4. Next, add the desired Java version to jEnv using the following command:
«`
jenv add /path/to/desired/java/version
«`
Replace «/path/to/desired/java/version» with the actual path to the desired Java version’s installation directory.
5. Finally, set the global Java version using the following command:
«`
jenv global
«`
Replace «» with the desired Java version number.
6. Verify that the Java version has been successfully changed by typing «java -version» in Command Prompt.
Pros:
1. Provides a convenient way to manage multiple Java versions
2. Easy to switch between different Java versions using jEnv commands
3. Works independently of system environment variables
Cons:
1. Requires installing and configuring jEnv
2. May not be suitable for users who prefer native Command Prompt functionality
3. Additional learning curve for using jEnv commands effectively
Method 4. Changing Java Version Using SDKMAN!
To change the Java version using SDKMAN!, follow these steps:
1. Download and install SDKMAN! from the official SDKMAN! website.
2. Open Command Prompt and type the following command to verify the successful installation:
«`
sdk version
«`
3. Install the desired Java version using the following command:
«`
sdk install java
«`
Replace «» with the desired Java version number.
4. Set the installed Java version as the default using the following command:
«`
sdk default java
«`
Again, replace «» with the desired Java version number.
5. Verify that the Java version has been successfully changed by typing «java -version» in Command Prompt.
Pros:
1. Provides a convenient way to manage multiple Java versions
2. Easy installation and management of Java versions with SDKMAN! commands
3. Works independently of system environment variables
Cons:
1. Requires installing and configuring SDKMAN!
2. May not be suitable for users who prefer native Command Prompt functionality
3. Additional learning curve for using SDKMAN! commands effectively
Why Can’t I Change the Java Version?
There can be several reasons why you may be unable to change the Java version on Windows 10 Command Prompt. Here are a few common reasons and their fixes:
1. Reason: Incorrect Java installation path specified.
Fix: Ensure that the correct path to the desired Java version’s installation directory is provided in the Command Prompt commands.
2. Reason: Missing or outdated Java installations on your computer.
Fix: Download and install the desired Java version from the official Java website before attempting to change the Java version on Command Prompt.
3. Reason: Environmental variables not properly modified.
Fix: Check the modification of the «Path» variable or other relevant environment variables and ensure that they accurately reflect the desired Java version’s installation directory.
Additional Tips
Here are some additional tips to enhance your experience when changing the Java version on Windows 10 Command Prompt:
1. Double-check the Java version: Always verify that the Java version has been successfully changed by running the «java -version» command after following any of the methods described above.
2. Use symbolic links: If you frequently switch between different Java versions, consider creating symbolic links to the Java installations and managing them using batch files or scripts.
3. Document your changes: Keep track of the Java version changes you make on Command Prompt by documenting them in a text file or using version control software. This will help you revert any changes or troubleshoot if needed.
5 FAQs about Changing Java Version on Windows 10 Command Prompt
Q1: Can I have multiple Java versions installed on my Windows 10 computer?
A1: Yes, it is possible to have multiple Java versions installed on your Windows 10 computer. This allows you to switch between different Java versions based on your needs.
Q2: Can I permanently set a default Java version on Command Prompt?
A2: Yes, you can permanently set a default Java version on Command Prompt by modifying system environment variables or using third-party tools like jEnv or SDKMAN!.
Q3: Are there any risks involved in changing the Java version on Command Prompt?
A3: Changing the Java version on Command Prompt does not pose significant risks. However, modifying system environment variables incorrectly can cause system-wide issues. Always double-check your changes and follow the provided instructions carefully.
Q4: Can I change the Java version for specific applications only?
A4: Yes, it is possible to change the Java version for specific applications by modifying environment variables or using tools like jEnv or SDKMAN!. These methods allow you to specify different Java versions for different applications.
Q5: Is it necessary to restart my computer after changing the Java version on Command Prompt?
A5: No, you do not need to restart your computer after changing the Java version on Command Prompt. The changes take effect immediately within the Command Prompt session.
In Conclusion
Changing the Java version on Windows 10 Command Prompt is a valuable skill for Java developers and enthusiasts. By following the methods outlined in this blog post, you can seamlessly switch between different Java versions, ensuring compatibility and enabling flexibility in your development projects. Remember to double-check your changes and use additional tools like jEnv or SDKMAN! for a more streamlined Java version management experience. Happy coding!
Changing the Java version on Windows 10 can seem like a daunting task, but it’s actually quite straightforward. By following a few simple steps, you can switch to the desired Java version without any hassle. This guide will walk you through the process step-by-step. Whether you’re a developer needing a specific version or just someone looking to update, this tutorial will have you up and running in no time.
In this step-by-step guide, you’ll learn how to change the default Java version on your Windows 10 machine. This process involves downloading the desired version, installing it, and then updating system environment variables.
Step 1: Download the Desired Java Version
First, download the Java version you need from the official Oracle website or any other trusted source.
Make sure you choose the correct version for your system (32-bit or 64-bit). Save the installer to a location you can easily access.
Step 2: Install the Java Version
Run the downloaded installer and follow the on-screen instructions to install the new Java version.
The installation process is straightforward. Just keep clicking «Next» and eventually «Finish.» Make a note of the installation directory.
Step 3: Open System Environment Variables
Press Win + S
, type «Environment Variables,» and select «Edit the system environment variables.»
This will open the System Properties window. Click on the «Environment Variables…» button in the lower-right corner.
Step 4: Edit the JAVA_HOME Variable
In the Environment Variables window, click «New» under System variables. Enter JAVA_HOME
as the variable name and the path to the new Java installation as the variable value.
This step sets the new Java version as the default Java for your system. Make sure the path is correct.
Step 5: Update the PATH Variable
Find the PATH variable in the System variables section and click «Edit.» Add %JAVA_HOME%bin
to the list of paths.
This tells the system where to find the Java executable. Make sure it’s placed at the beginning of the list for priority.
Step 6: Verify the Java Version
Open Command Prompt and type java -version
to verify that the new Java version is now the default.
If everything is set up correctly, you should see the new version number displayed. If not, double-check your environment variables.
After completing these steps, your system will use the new Java version by default. You can always switch back or change to another version using the same steps.
Tips for Changing Java Version in Windows 10
- Backup Your System: Always create a system restore point before making changes to system environment variables.
- Use Version Managers: Tools like
jEnv
can help manage multiple Java versions easily. - Check Compatibility: Make sure the new Java version is compatible with your applications.
- Uninstall Old Versions: Remove older Java versions if you don’t need them to free up space.
- Environmental Variables: Double-check your environment variables to ensure they are typed correctly.
Frequently Asked Questions
What is JAVA_HOME?
JAVA_HOME is an environment variable that points to the directory where Java is installed. It’s used by various applications to locate the Java runtime.
Can I have multiple Java versions on Windows 10?
Yes, you can install multiple Java versions, but you need to update the JAVA_HOME and PATH variables to switch between them.
How do I uninstall an old Java version?
You can uninstall an old Java version through the Control Panel under «Programs and Features.»
Why isn’t my new Java version recognized?
Ensure the new Java path is correctly set in the JAVA_HOME and PATH variables, and that they are placed at the beginning of the PATH list.
Do I need administrative rights to change Java versions?
Yes, you typically need administrative rights to modify system environment variables.
Summary
- Download the desired Java version.
- Install the Java version.
- Open System Environment Variables.
- Edit the JAVA_HOME variable.
- Update the PATH variable.
- Verify the Java version.
Conclusion
Changing the Java version in Windows 10 is a straightforward process, but it requires careful attention to detail. By following the steps outlined above, you can switch between different Java versions with ease. This is particularly useful for developers who need specific Java versions for different projects.
Remember, always double-check your environment variables to avoid any mistakes. Feel free to explore tools like version managers if you frequently switch between Java versions. Keep your system and projects running smoothly by staying up-to-date with the latest Java updates and best practices. 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 the backbone of countless applications and tools, yet dealing with its setup on Windows can feel like wrestling with a greased-up octopus. Fear not, though! Whether you’re checking your Java version, updating it, or setting the ever-elusive JAVA_HOME environment variable, this guide will make you feel like a Java ninja—sarcastic commentary included (free of charge).
How to Check the Java Version on Windows
Let’s start with the basics. You can’t manage your Java setup if you don’t know what you’re working with. Checking your Java version is like finding out which friend brought the weird casserole to the potluck: it’s critical information.
- Open Command Prompt:
PressWin + R
, typecmd
, and hit Enter. Alternatively, search for “Command Prompt” in the Start menu. - Type the Magic Words:
Type the following command and hit Enter:java -version
- Interpret the Result:
You’ll see something like this:java version "17.0.3" 2021-09-14 LTS Java(TM) SE Runtime Environment (build 17.0.3+7-LTS) Java HotSpot(TM) 64-Bit Server VM (build 17.0.3+7-LTS, mixed mode)
The first line tells you the installed Java version. If you see an error message likejava is not recognized
, congratulations—you don’t have Java installed (or it’s hiding somewhere deep in the bowels of your PATH).
How to Check the JDK Version on Windows
If you’re working with Java Development Kit (JDK), the process is eerily similar:
- Open Command Prompt:
You know the drill—Win + R
, typecmd
, Enter. - Run the Command:
Enter:javac -version
- Read the Output:
You’ll see something like this:javac 17.0.3
If this doesn’t work, your JDK isn’t installed or your PATH hasn’t been configured properly. No judgment—fixing it is our next mission.
How to Change the Java Version on Windows
Let’s say you have multiple Java versions installed because, hey, why not make life complicated? Here’s how to switch between them:
- Check Installed Versions:
Locate all installed Java versions. These are usually inC:\Program Files\Java
. - Update the PATH Variable:
- Press
Win + S
and search for Environment Variables. - Under System Variables, find
Path
and click Edit. - Remove the current Java path and add the one you want to use (e.g.,
C:\Program Files\Java\jdk-17.0.3\bin
). - Click OK and restart any open Command Prompt sessions.
- Press
- Verify the Change:
Runjava -version
again to confirm the active Java version.
How to Set JAVA_HOME on Windows
Ah, the infamous JAVA_HOME
. Setting this is like deciphering ancient hieroglyphs, but once you master it, you’ll feel invincible.
- Locate Your Java Installation Directory:
Typically found inC:\Program Files\Java\jdk-XX
, whereXX
is your JDK version. - Open Environment Variables:
- Press
Win + S
, type Environment Variables, and open it. - Under System Variables, click New.
- Press
- Create the Variable:
- Name it
JAVA_HOME
. - Set the value to your Java installation directory (e.g.,
C:\Program Files\Java\jdk-17.0.3
).
- Name it
- Add JAVA_HOME to PATH:
- In the same Environment Variables window, edit the
Path
variable. - Add
%JAVA_HOME%\bin
to the list.
- In the same Environment Variables window, edit the
- Verify It Works:
Open Command Prompt and type:echo %JAVA_HOME%
If it spits out the correct path, congratulations—you’ve done it!
How to Update Java on Windows 11
Updating Java doesn’t have to be a pain (unless you enjoy unnecessary stress, in which case, proceed without reading this).
- Check for Updates:
- Visit the Java Downloads Page.
- Compare your installed version (
java -version
) with the latest available.
- Download the Latest Version:
Choose the correct version for your system (x64 for most modern PCs). - Install the Update:
Run the installer and follow the prompts. During installation, you can opt to remove older versions if you’re feeling bold. - Update PATH and JAVA_HOME:
Repeat the steps above to update your environment variables if needed. - Verify the Update:
Usejava -version
to confirm you’re running the latest version.
Quick Comparison of Commands and Tasks
Task | Command/Steps | Difficulty Level |
---|---|---|
Check Java version | java -version |
Easy |
Check JDK version | javac -version |
Easy |
Change Java version | Update PATH variable | Medium |
Set JAVA_HOME | Create and configure variable | Medium |
Update Java | Download, install, reconfigure | Medium |
Final Thoughts
Dealing with Java on Windows can seem daunting at first, but with a bit of patience (and coffee, lots of coffee), it’s manageable. Whether you’re a developer, a student, or just someone trying to run that one weird application that only works with Java 8, these steps will keep you on track.
Now go forth, command-line warriors, and conquer Java setup like the coding pros you are—or at least, like someone who knows their way around cmd
.
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:
- Java Development Kit (JDK): This is what developers need to create Java applications.
- Java Runtime Environment (JRE): This is necessary for running Java applications.
- 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.
-
Open Command Prompt: Press
Win + R
, typecmd
, and hitEnter
. -
Check Installed Versions: In the Command Prompt, type:
java -version
This command will display the currently active Java version on your system.
-
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.
-
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
-
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. -
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.
-
Open System Properties:
- Press
Win + R
to open the Run dialog. - Type
sysdm.cpl
and pressEnter
. This will open the System Properties window.
- Press
-
Go to Environment Variables:
- Click on the
Advanced
tab. - At the bottom, click on
Environment Variables
.
- Click on the
-
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 onNew...
to create it.
- In the Environment Variables window, under System variables (the bottom section), check if there is a variable named
-
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
).
- Set the variable name as
-
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 clickEdit
. - In the Edit Environment Variable window, click
New
and add the path to the JDK’sbin
folder (e.g.,C:Program FilesJavajdk-17bin
). - Click
OK
to close all dialog boxes.
- Now, you need to update the
Validating the Java Version Change
After changing the environment variables, you will want to verify that your system recognizes the new Java version.
-
Reopen Command Prompt: Close any open Command Prompt windows and open a new one, as changes will not reflect in already opened terminals.
-
Check Java Version: Again, type:
java -version
This should now display the version you installed and configured in your environment variables.
-
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:
- Open Command Prompt.
- Set JAVA_HOME locally:
set JAVA_HOME=C:Program FilesJavajdk-11
- 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.
-
Installing SDKMAN!:
- Open a terminal and use the following command to install it:
curl -s "https://get.sdkman.io" | bash
- Open a terminal and use the following command to install it:
-
Follow the Instructions: Follow the on-screen instructions to complete the installation.
-
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
-
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
orjavac
is not recognized, it’s likely due to thePATH
variable not being set correctly. Double-check thebin
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!