java -version is running the wrong version of java.

Diagnostics:

>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)

the following is the Java related contents from the output of PATH:

PATH=C:\ProgramData\Oracle\Java\javapath; ... C:\Program Files\Java\jdk1.6.0_45\bin

Conclusion:

From the above output we can deduce that C:\ProgramData\Oracle\Java\javapath is 1.8.0_66.

You need to change your PATH to put C:\Program Files\Java\jdk1.6.0_45\bin first.

I noticed that after checking the path per your suggestion. Windows 10 does not allow me to edit the path because it says "This environment variable is too large." I know there should be another question to deal with this separately.

You also need to clean up your path. My guess is you have a lot of duplicate entries.

Answer from DavidPostill on Stack Exchange
🌐
HappyCoders.eu
happycoders.eu › java › how-to-switch-multiple-java-versions-windows
How to Change Java Versions in Windows (up to Java 25)
June 11, 2025 - This is one of the few posts ive seen that both provides a great description for how to set up java, as well as the easiest way ive seen to switch java versions on the fly. 10/10 would recommend to my whole company (already have ;)) ... Its working fine. ... This is a different approach that does not work natively on Windows.
Discussions

Unable to change Java Version in windows - Stack Overflow
Previously I am using Java 1.8 in my machine. But now i need to use Java 1.6. So I changed the below values in system environment variables. JAVA_HOME U:\POC\jdk1.6.0_31 PATH U:\POC\jdk1.6.0_31... More on stackoverflow.com
🌐 stackoverflow.com
How do I switch between Java versions?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
13
11
March 20, 2023
Switch between java versions with a command on Windows
So is there a way I can switch between my instaled jdks? For example if I have installed jdk8 and jdk16 I can run a batch file and switch the version… More on reddit.com
🌐 r/javahelp
14
3
September 21, 2021
Windows 10 change java version? - Stack Overflow
Im working with Intellij IDEA + JDK 14 and want to execute my project as jar in my console. Herefor I use java -jar pathToJar There is always the following Error message : Exception in thread &quo... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 7
16

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.

2 of 7
5

I created scripts for every version of Java that I have installed on my PC, and use those whenever I need to change the Java version through the command line. Here is an example of one, should be self-explanatory really:

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk-10
echo Setting PATH
set PATH=C:\Program Files\Java\jdk-10\bin;%PATH%
echo Display java version
java -version
🌐
Chetangole
chetangole.com › blogs › change current java version in windows 10 in one click - using a batch file
Change Current Java Version in Windows 10 in one click - using a batch file | Chetan Gole
So I created this simple batch file to change the Java version with one click. Create a new “.bat” file say for example 8_java.bat, replace your Java version path in the below code and use it in the batch file. @echo off echo Current Java is echo %JAVA_HOME% echo Setting new Java version as setx -m JAVA_HOME "C:\Program Files\Java\jdk<replace_your_version>" echo %JAVA_HOME% echo Restart the session pause
🌐
Support Your Tech
supportyourtech.com › home › articles › how to change java version on windows 10: a step-by-step guide
How to Change Java Version on Windows 10: A Step-by-Step Guide
September 4, 2024 - Changing the Java version on Windows 10 is easier than you might think. All you need to do is download and install the desired version, update your environment variables, and verify the change.
🌐
Opt Node
optnode.com › home › guides › how to change java version in windows 10: a simple guide
How to Change Java Version in Windows 10: A Simple Guide - Opt Node
January 13, 2026 - Changing the Java version in Windows 10 might sound complex, but it’s actually a straightforward process. You’ll just need to adjust some environment variables and ensure your system recognizes the new version.
Find elsewhere
🌐
Medium
medium.com › @pabasarapasindu365 › effortlessly-switch-between-multiple-java-versions-in-the-same-command-prompt-on-windows-03adc8d1e96e
Effortlessly Switch Between Multiple Java Versions in the Same Command Prompt on Windows.
July 18, 2024 - In this tutorial, I’ll show you how to effortlessly switch between multiple Java versions in the same command prompt using simple batch scripts.
🌐
YouTube
youtube.com › code with arjun
How to switch between the multiple Java versions(JDK) in windows 10 | Switch between java 8,11,15,17 - YouTube
In this Video i am going to show how to switch or manage the multiple java versions of jdk versions at a time in windows operating system. If you have java 8...
Published   June 25, 2021
Views   145K
🌐
University of Oregon
service.uoregon.edu › TDClient › 2030 › Portal › KB › ArticleDet
Reverting to a Previous Version of Java (Windows)
Please note these instructions focus on Java 8. ... In Windows 10 click in the search box on the bottom left corner of the taskbar (either Cortana or the magnifying glass) and type Control Panel.
🌐
Reddit
reddit.com › r/javahelp › switch between java versions with a command on windows
r/javahelp on Reddit: Switch between java versions with a command on Windows
September 21, 2021 - Yo wtf, are they stupid or what, but yeah you could have a script for this and just invoke an script every time for an exact version of java aka: runJ8.bat and inside of it you have directory to javac and you are done ... Use the release switch. This way, you keep whatever jdk version you want, but you get source and class files for a target version: ... Oh, i didn't know that. Thanks! ... While it's on my mind, I think the release switch was introduced in jdk 9 or 10.
Top answer
1 of 3
2

It is very simple:

  • either you tell IntelliJ to compile your classes to java 8 bytecode
  • or you ensure that your windows command line is pointing to your java 11 setup

For Intellij; you look into "Project Settings", then turn to "SDKs" under Platform Settings. You could point that to a JDK8 installation.

Alternatively, as you probably installed Java 14 on purpose: locate where it sits on your disk, and ensure your Windows PATH variable points to that location. Or just put some "wrapper" script somewhere into your PATH, and have that wrapper script call the "java 14" java.

2 of 3
0

You program didn't work because your computer points to an old version of java JDK in PATH. You can just uninstall Java 8 and reinstall java 14. In cases, if don't want to delete the old one or you have manually changed the PATH variable, then:

Step 1: Delete old items of Java JDK from PATH.

Type "this pc" in the search bar next to the start button -> Right click "This PC", choose "Properties" -> click "Advanced system settings" on the left side bar -> click "Advanced" tab -> click "Environment Variables" button at bottom -> Modify the "Path" variable under System variables panel and DELETE all your old java path information(or any item in other variables with your old java information). If you done right, java -version should not work right now.

Step 2: install Java JDK 14 from installer

New Java PATH info will be automatically added to the PATH environment variable. You don't need to manually update anything. (If you do want, follow step 1 to change PATH)

Top answer
1 of 9
58

The set command only works for the current terminal. To permanently set a system or user environment variable you can use setx.

You can set the variable for the current user like this:

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72"

You can also set the variable system wide (Note: The terminal must be run as administrator fo this) by running the same command with the /m option:

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72" /m

The variable will be available in all new terminal session, but not the current one. If you also want to use the path in the same session, you need to use both set and setx.

You can avoid manipulating the PATH variable if you just once put %JAVA_HOME%\bin in there, instead of the full JDK path. If you change JAVA_HOME, PATH will be point to the new location too.


There are also a few environment variable editors as alternative to the cumbersome Windows environment variable settings. See "Is there a convenient way to edit PATH in Windows 7?" on Super User.

2 of 9
19

In case if someone want to switch frequently in each new command window then I am using following approach.

Command Prompt Version:

Create batch file using below code. You can add n number of version using if and else blocks.

@echo off
if "%~1" == "11" (
   set "JAVA_HOME=C:\Software\openjdk-11+28_windows-x64_bin\jdk-11"
) else (
   set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151"
)
set "Path=%JAVA_HOME%\bin;%Path%"
java -version

Save this batch file as SJV.bat and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".

Now open new command window and just type SJV 11 it will switch to Java 11. Type SJV 8 it will switch to Java 8.

PowerShell Version

Create powershell(ps1) file using below code. You can add n number of version using if and else blocks.

If($args[0] -eq "11")
{
    $env:JAVA_HOME = 'C:\Software\openjdk-11+28_windows-x64_bin\jdk-11'
}else{
    $env:JAVA_HOME = 'C:\Program Files\Java\jdk1.8.0_151'
}
$env:Path = $env:JAVA_HOME+'\bin;'+$env:Path
java -version

Save this script file as SJV.ps1 and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".

Now open new powershell window and just type SJV 11 it will switch to Java 11. Type SJV 8 OR SJV it will switch to Java 8.

I hope this help someone who want to change it frequently.

🌐
Ultimate Systems Blog
blog.usro.net › ultimate systems blog › how-to › windows › how to change, set, check, and update java on windows
How to Change, Set, Check, and Update Java on Windows – Ultimate Systems Blog
November 17, 2024 - Locate Your Java Installation Directory: Typically found in C:\Program Files\Java\jdk-XX, where XX is your JDK version. ... Press Win + S, type Environment Variables, and open it. Under System Variables, click New.
🌐
Oracle
java.com › en › download › help › update_runtime_settings.html
Update Java runtime settings on Windows
Verify that the latest Java Runtime version is enabled by checking the Enabled box. ... Click OK in Java Control Panel window to confirm changes and close the window.
🌐
Damir's Corner
damirscorner.com › blog › posts › 20251226-SwitchingBetweenJdksInWindows.html
Switching between JDKs in Windows | Damir's Corner
December 26, 2025 - I decided to use JDK 25 by default, so I made the following changes to my user environment variables: I set JAVA_HOME to the JDK path: C:\Users\damir\.jdks\openjdk-25.0.1. I added the bin subfolder inside it to PATH: C:\Users\damir\.jdks\openjdk-25.0.1\bin. This was enough to get Java 25 working in a newly opened terminal: ➜ java -version openjdk version "25.0.1" 2025-10-21 OpenJDK Runtime Environment (build 25.0.1+8-27) OpenJDK 64-Bit Server VM (build 25.0.1+8-27, mixed mode, sharing)
🌐
YouTube
youtube.com › ritman
How to Update Java version on Windows 10 - 64 bit | Upgrade Java Version - YouTube
In this video tutorial I will show you how to check and update/upgrade your Java version to the latest version on Windows 10, So you have latest version the ...
Published   January 27, 2024
Views   10K
🌐
javaspring
javaspring.net › blog › change-java-version-windows
Changing Java Version on Windows: A Comprehensive Guide — javaspring.net
For example, to switch to Java 11, set the value to C:\Program Files\Java\jdk - 11.0.10. In the same "Environment Variables" window, find the PATH variable and click "Edit". Locate the entry that points to the bin directory of the old Java version and replace it with the bin directory of the new Java version.