Switch JDK version in Windows 10 cmd
Is there a way to change JDK version easily in cmd? like the version on mac. Change Default JDK on Mac.
asked Nov 24, 2017 at 8:37
453 1 1 gold badge 4 4 silver badges 15 15 bronze badges
7 Answers 7
Here’s my guide for Windows 10.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as JAVA_8_HOME
- JAVA_8_HOME : %ProgramFiles%\Java\jdk1.8.0_152\bin
- JAVA_9_HOME : %ProgramFiles%\Java\jdk-9.0.1\bin
- JAVA_HOME : %JAVA_8_HOME%
In my case, JAVA_8_HOME (JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin . You can replace this with your own path to javac . %JAVA_HOME% has a default value pointing to JAVA_8_HOME , which is the path for JDK8. That’s my preference, feel free to adjust accordingly.
Step 3. Select PATH and click on Edit . PATH
Step 4. Click on New and add %JAVA_HOME%. %JAVA_HOME% will be added to PATH automatically every time you launch a command prompt.
In order to switch JDK version in cmd, here’s the trick. Step 5. I created a batch file with
@echo off :: Switch JDK version DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%; DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, java8 or java9 is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat . You can name it whatever you want.
Step 6. Search for regedit (Registry Editor). Click on Edit > New > String Value . Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data . Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat , which creates those two alias in the script.
Step 7. Close any command prompt you’re using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you’re using different Python versions, same trick applies, too. Find my python environment settings here.
Как поменять версию java
I suggest you unpack the scripts to C:\Program Files\Java\scripts .
The scripts look like this:
@echo off call javaX "Java 20" %1Code language: DOS .bat (dos)
javaX "Java 20" $args[0] Code language: PowerShell (powershell)
@echo off if %1 = hljs-number">1.2" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.2.2 if %1 = hljs-number">1.3" set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.3.1_28 . if %1 = hljs-number">20" set JAVA_HOME=C:\Program Files\Java\jdk-20 if %1 = hljs-number">21" set JAVA_HOME=C:\Program Files\Java\jdk-21 if "%~2" == "perm" ( setx JAVA_HOME "%JAVA_HOME%" /M ) set Path=%JAVA_HOME%\bin;%Path% echo %~1 activated.Code language: DOS .bat (dos)
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 20" < $env:JAVA_HOME = "C:\Program Files\Java\jdk-20" > "Java 21" < $env:JAVA_HOME = "C:\Program Files\Java\jdk-21" > > 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 20, 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.
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.
How to Change Java Version on Windows 10 Command Prompt
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!
Related Posts:
- can minecraft java play with windows 10?
- How to Check Java Version on Windows Server 2012 R2
- How to Change Default Java Version on Mac
- how to create java_home environment variable in windows 10?
- How to Run Java ProgRAM on Windows 10 Command Prompt
- how to uninstall sdkman on mac?
- how to run minecraft java edition on windows 10?
- how to setup java_home in windows 10?
- how to change java security settings mac?
- is minecraft windows 10 java?
How to change Java version on Windows 10
I have done the following: 1. Set the environment variable JAVA_HOME:
2. Add Java 1.6.0_45 and disable Java 1.8.0_66 in Java Runtime Environment Settings under Configure Java:
Unfortunately, the Java is still 1.8.0_66:
>java -version java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)
Could anyone offer a tip on this? Edit: Per David’s suggestion, the following is the Java related contents from the output of command PATH (the entire output is super long, I hope the following is sufficient for this question.):
PATH=C:\ProgramData\Oracle\Java\javapath; . C:\Program Files\Java\jdk1.6.0_45\bin