java -version showing different version than in path - java

I have jdk12 and jdk8 on my computer installed. Usually jdk12 is set in the path.
To run some java programs I had set path to jdk8. Now after changing the path back to /jdk-12.0.1/bin, running java -version on cmd still returns
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

I decided to assemble my comments to an answer:
When calling java on the command line, the OS only uses the PATH environment variable to look for the command. The variable JAVA_HOME is not used here.
So you simply should examine the PATH variable: The directory C:\Program Files (x86)\Common Files\Oracle\Java\javapath is added to the PATH by the JDK installer. I would suggest to remove it, so you have better control what directories are part of the PATH.

If you have multiple Java installed, create the JAVA_HOME variable and give the JAVA path which you want to use. Use only this JAVA_HOME variable in the PATH variable.
If you wish to change the default Java, change only the JAVA_HOME variable pointing to the required JAVA.
This way, only one JAVA (which is required) will be present in PATH. Remove all other JAVA references in the PATH variable.
Also, whenever you change the JAVA_HOME value, open a new terminal (command prompt) to verify this change.
Note: If the path contains spaces, use the shortened path name. For example, C:\Progra~1\Java\jdk1.8.0_65

Related

How to know the jdk version on my machine?

I have recently uninstalled JDK 11 and installed JDK 8. For confirmation, I want to check which JDK is installed on my Windows 10 machine. I typed java -version on cmd then get the error message
java is not recognized as an internal or external command
How to know which JDK version installed on my PC?
you might need to add path in environment variables which you can find in Control Panel
open the Jdk where you installed and add until /bin in the path in environment variables.
Add until /bin in path variable in System Variables which is residing in Environment Variables.
Then do
java -version
which might show up.
If still problem persists, try restarting your pc and see.
You need to update your Windows path to include your %JAVA_HOME%\bin directory. %JAVA_HOME% is the directory that you installed Java into and is also an environment variable that you need to configure for command line execution of your applications. You can edit both of these in the Windows control panel and you should restart.
When you run java -version you will see the internal version number. This is explained here: https://en.wikipedia.org/wiki/Java_version_history.
Basically, you can ignore the 1. when reading version number. The _xxx is a reference to the most recent patch or build release.
On Windows 10, this required mapping the environment variable for JAVA_HOME to the JDK installation directory. Use these steps:
Run the installer for the JDK. (available for windows here: https://www.oracle.com/java/technologies/downloads/#jdk17-windows)
windows key -> Environment Variables, select the only result
In the System Properties window that opened, select Environment Variables
Select new button under the User variables section
Variable name: JAVA_HOME, Variable Value: <The JDK filepath from step 0>
ok all open menus
Close any open cmd prompt windows
open a new cmd window and type echo %JAVA_HOME% It should print the installation path for the JDK.
To get your jdk location in Windows, run this at a command prompt:
where java
This lists any and all locations of java.exe, including from your JAVA_HOME. For example, the 3rd line here reflects my JAVA_HOME location, where I'm pointing to JDK 8:
C:\Users\me> where java
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk1.8.0_202\bin\java.exe
Note for comparison that java -version does not reflect my JAVA_HOME location and in fact shows java version 11 instead of 8:
C:\Users\me> java -version
java version "11.0.15" 2022-04-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.15+8-LTS-149)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.15+8-LTS-149, mixed mode)
This is confusing because my Java compiles (e.g., via mvn) use JDK 8 since that's what my JAVA_HOME is pointing to. (I'm not even sure where the version 11 it found came from; possibly from when I installed maven.)
Determining the difference between the JRE and JDK you're running has never been straightforward. Seems like java -version used to be a way to do this, but no longer.
Adding to the complexity, you can also supposedly get your Java version info from Control Panel > Programs > Java > About. For me, that shows Version 8. That's despite java -version showing version 11.0.15. And it doesn't change even if I point my JAVA_HOME to JDK 11.
Note that this answer is also helpful. In my case, that helped me determine that I have java.exe and javac.exe at C:\Program Files (x86)\Common Files\Oracle\Java\javapath and C:\Program Files\Common Files\Oracle\Java\javapath. Depending on which one I have listed first in my Path variable, I get different results when i run java -version or java --version. The former seems to work when Java 8 is listed first; the latter when Java 11 is first.

Getting different version for java and javac

Before marking it duplicate FYI I have already read How can I change the Java Runtime Version on Windows (7)? and How to set path for Jre 6 when jre 7 installed?.
I have both Java7 and Java8 in my machine. I have
JAVA_HOME = C:\Program Files\Java\jdk1.7.0_60
PATH = ...;%JAVA_HOME%\bin;....
but I am getting
C:\>java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
C:\>javac -version
javac 1.7.0_60
I want to set JRE to 1.7 too. How to do that?
You will probably have to edit your path environment variable.
If you want to check what's in your PATH environment variable in an organized fashion, run:
echo %path:;=&echo.%
Make sure the 1.8 is not on the path or if you want it there make sure it appears after 1.7.
To edit it, go to My Computer > Properties > System Properties > Advanced tab > Advanced section > Environment Variables.
Also check what is actually launched when you run java from the command line, run the following command:
where java
It will show you what windows runs when you request java.
To solve the problem, remove or change the name of java.exe and javaw.exe into System32 folder:
Prepend the System Variable's Path with JRE bin's path.
where java did work for me. I found there is another version of java associated with the SPSS (1.8.0). Uninstall SPSS and the problem solved.
The solution that worked for me was:
In "Path" variable replace "C:\ProgramData\Oracle\Java\javapath;" with %JAVA_HOME%\bin where JAVA_HOME variable was set to "C:\Program Files\Java\jdk1.7.0_60"

Two installed javas on same pc. The one with lower version can not be called

I have two javas installed on my pc. jdk1.7.0_45 (x64) and jdk1.6.0_45(x86). I want that default java on my machine would be jdk1.6.0_45(x86). I added JAVA_HOME with value C:\Program Files (x86)\Java\jdk1.6.0_45 and added C:\Program Files (x86)\Java\jdk1.6.0_45\bin to system path, but still when i type in command prompt "java -version" it says:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
I have no idea why the things are like that, because my OS should see jdk1.7.0_45 (x64) version. Why is that so?
I use windows 8
edit:
after setting java home to jdk1.7.0_45 (x64) folder and updating path accordingly javac works, but then I set java home and path to jdk1.6.0_45(x86) javac does not work.
You need to add the %JAVA_HOME%\bin to the very beginning of your path. This is because Java is installed in your %SystemRoot%\system32, too. If you add the %JAVA_HOME%\bin to the beginning of your path the Java installed in your JAVA_HOME will be used.
JAVA_HOME does not help when we run java from command line, you should fix PATH system var
I added JAVA_HOME with value C:\Program Files (x86)\Java\jdk1.6.0_45
In Windows, you should excape spaces in the path: JAVA_HOME="C:\Program Files (x86)\Java\jdk1.6.0_45". But the best way is to set Java to a folder that doesn't have spaces in its path. For example, I use C:\Java\Java6
and added C:\Program Files (x86)\Java\jdk1.6.0_45\bin to system path
Probably you added after path to the Java 7. Make sure that Java 7 doesn't exist in the system path.

java -version doesn't display the value of JAVA_HOME

I've installed jdk 7 and set up the "JAVA_HOME" to this version. Then, I've installed jdk 6 and I've updated the "JAVA_HOME" to point to jdk 6. After closing and reopening the system, when typing "java -version", I got always :
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) Client VM (build 24.0-b56, mixed mode, sharing)
However, when typing
echo %JAVA_HOME%
I got :
C:\Program Files\Java\jdk1.6.0_45
I didn't understand why java -version don't display the value of "JAVA_HOME"
Java 7 puts a java.exe in c:\windows\system32. You could try to delete these exe's, but I'm not sure Windows will allow it, or restore it after a reboot.
If you want to override it, you must put %JAVA_HOME%\bin as first entry in your PATH, before c:\windows\system32.
Which java starts when you run java -version depends on the PATH env variable, not JAVA_HOME. OS will be looking for java.exe (Windows) like for any other program
if you run java on your command window, it takes java from the PATH variable. JAVA_HOME is used my maven etc...
I believe your PATH variable is pointing to JAVA version 1.7.0_40/bin directory.
According to my understanding, this is nothing to do with your JAVA_HOME environment variable. You are getting java version from C:\windows\system32\java.exe.
If you don’t want that behaviour then in system variables section put %JAVA_HOME%\bin as starting element (Of course you should have JAVA_HOME). (I thought of adding image but I dont have enough reputation to do so :( )

Javac and java pointing to different environments

Please Help,
I am trying to run a compiled java class and getting errors but when I try to check my java environments it points different ways as seen below
c:\NetBeansProjects\Hello\src>javac -version
javac 1.7.0
c:\NetBeansProjects\Hello\src>java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)
According to my PC(windows 7) I have
C:\Program Files (x86)\Java
jdk1.6.0_25
jdk1.7.0
jre6
jre7
How can I point it all to Java 7 or only Jave 6.....just want to try java 7 to see the fastness compared to java 6...hope all I have written helped.
Cheers.
Look at your path - I suspect c:\Windows\System32 is ahead of the JDK7 directory... and I suspect that's Java 6 for whatever reason.
You have the jre/bin directory on the system path before the jdk/bin. The javac command doesn't exist in the jre installation.
Thus the java command gets the version under jre6 but javac gets the version under jdk1.7.0.
You should change your system path to only include the one you want. If you want to explicitly use one over the other use the absolute name (including path) instead of just the executable name.
In the PATH variable enter C:\Program Files (x86)\Java\jdk1.6.0_25\bin
before the path of system32.
It fixed my problem
Just make sure java's path is the first path in the "PATH" environment variable
In all likelyhood, you have installed a JDK 7 and a JRE 6 and in your PATH environment variable the JRE bin path is before your JDK bin path
I'ts just because of your path, JRE does not contain javac and it contains java, so in your path the JRE must be located before the JDK

Categories