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 Exchangejava -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.
I have the same problem, I have set JAVA_HOME:
C:\Program Files\Java\jdk1.7.0_75
and Path to:
%JAVA_HOME%\bin
I need run jdk 7. When I run java -version it always appear jdk 8.
I solved it with: in System Environment --> Path --> order %JAVA_HOME%\bin to first.
java - Switch JDK version in Windows 10 cmd - Stack Overflow
Unable to change Java Version in windows - Stack Overflow
How do I switch between Java versions?
Switch between java versions with a command on Windows
Videos
Change your PATH variable so that it has the location of the jdk5/bin directory:
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
- Close the window.
- Reopen Command prompt window, and run
java -version
In the command shell:
set JAVA_HOME=C:\jdk1.6.0u24
set PATH=%JAVA_HOME%\bin;%PATH%
That will temporarily set up the environment in the command shell. Maven, Ant, etc. will pick up on your new version of Java without having to go to the Control Panel repeatedly.
Tools like Eclipse should be able to select which JDK to use in their own configuration tools for use within their environments.
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\binJAVA_9_HOME:%ProgramFiles%\Java\jdk-9.0.1\binJAVA_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.
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
when latest version is installed in system it automatically set javapath in environment variable like "C:\ProgramData\Oracle\java\javapath".
Remove this from environment variables.
This suggestion may seem obvious, but I urge you to carefully consider this advice.
I have encountered this issue before, and frequently, it has been due to an overlooked entry within the long list of items making up the Windows Path. It is often an path entry where it is not immediately obvious that any of the java executables (javaw.exe, javaw.exe or javaws.exe) may reside. A common culprit is C:\ProgramData\Oracle\Java\javapath
You will therefore need to push upwards, your desired java/bin path above these. If in doubt, push it right to the top!
In the unlikely event that this does not work, then try making changes from within the java admin console (Control Panel --> Java --> Java tab --> View Button).
So, I have two different versions of Java in my system, 8 and 16. Depending on the project, I need to switch between the two. Right now, I need to use 16, but when I run java -version I see, I'm still using "1.8.0_202".
I have changed my JAVA_HOME variable to point to the location of Java 16. Do I need to uninstall Java 8 in order to use Java 16? I'd rather not do that.
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.
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.
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.
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)