Why is java -version returning a different version to the one defined in JAVA_HOME?

19

5

My JAVA_HOME is set to C:\bea\jdk150_10\bin (Java 1.5). However, when I run java -version I get the following:

java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

How can I use the 1.5 version?

Jeune

Posted 2011-01-25T19:07:24.723

Reputation: 335

Answers

20

As Mikel has mentioned, In your machine java is taken from C:\Windows\System32\java.exe.

Change the Path in the System Environment variables as mentioned by John T.

And modify it have the C:\bea\jdk150_10\bin before %SystemRoot%\system32 in the path.

lmeyya

Posted 2011-01-25T19:07:24.723

Reputation: 316

15

Run

where java

at your command prompt.

On my system, it printed something quite unexpected:

C:\Users\Mikel>where java
C:\Windows\System32\java.exe

Verifying there really is a Java there:

C:\Users\Mikel>c:\windows\system32\java.exe -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

So one solution is to ensure your preferred Java is in the PATH before C:\Windows\System32.

I also tried looking in the Java control panel to see if it manages the version in C:\Windows\System32, but I could only view the settings, not edit them. Maybe it works for you?

Mikel

Posted 2011-01-25T19:07:24.723

Reputation: 7 890

8

Modify the PATH variable, not JAVA_HOME.

See the Java documentation on setting or changing your PATH variable.

  1. Start -> Control Panel -> System -> Advanced

  2. Click on Environment Variables, under System Variables, find PATH, and click on it.

  3. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. 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 class as the value.

  4. Close the window.

  5. Reopen Command prompt window, and run your Java code.

John T

Posted 2011-01-25T19:07:24.723

Reputation: 149 037

I don't see any references to jdk 6 on the PATH variable either. Only Java 5 X) – Jeune – 2011-01-25T19:29:44.017

It's an example, the procedure is still the same.. – John T – 2011-01-25T19:31:30.753

4

Not all applications use JAVA_HOME to get the JDK version. As said before, some of them use the %SystemRoot%\System32\java.exe. You can do %SystemRoot%\java.exe -version to see which is that version.

What I have to add to the other responses is that if you build with Maven, M2_HOME\bin has also to be before %SystemRoot% in the order for Maven to use the correct JAVA_HOME version.

Mihai Rus

Posted 2011-01-25T19:07:24.723

Reputation: 41

1

JAVA_HOME should not be pointing to the bin!

SET "JAVA_HOME=C:\bea\jdk150_10" SET "PATH=%JAVA_HOME%/bin;%PATH%"

Also make sure that the path to JAVA_HOME is correct! If there's a typo and C:\Windows\System32\ is in you path then that java.exe will get run instead of the one you specified.

Bad Karma

Posted 2011-01-25T19:07:24.723

Reputation: 11

0

For me on win 10, javac -version showed java7 and java -version showed java8.Doing "where java" on command prompt showed java at three locations. I had to remove Java from C:\Windows\System32, Remove entry of "C:\ProgramFile\Oracle\Java\javapath"and "C:\ProgramData\Oracle\Java" from path variable, keep only Jdk7 entry in path variable worked after two hour long struggle.

Sneha Rathod

Posted 2011-01-25T19:07:24.723

Reputation: 1

0

Go to advanced settings of environment variables and edit the path variable. whichever version of java you want just move up that versions path above all other environment variables of other java versions.

here in the image java_home environment variable is moved up than oracle java environment

nikita dharurkar

Posted 2011-01-25T19:07:24.723

Reputation: 1

-1

I was facing the same issue. Later I found that the culprit was Java 8.Earlier I installed java 8 but now there is no folder for java 8 and my JAVA_HOME set to jre7 but still I was facing above issue.

solution

I deleted javapath present in C:\ProgramData\Oracle\Java. Now my system working properly. It also resolved my eclipse startup problem.

Sudip7

Posted 2011-01-25T19:07:24.723

Reputation: 107

-1

Try this:

@echo off
DOSKEY WHICH=@for %%E in (%PATHEXT%) do @for %%I in ($*%%E) do @if NOT "%%~$PATH:I"=="" echo %%~$PATH:I
SET "JAVA_HOME=C:\Java\jdk1.6"
SET "PATH=%JAVA_HOME%\bin;%PATH%"
where.exe java
WHICH java
pause

djangofan

Posted 2011-01-25T19:07:24.723

Reputation: 2 459

-2

I had the same issue. Doing this I resolved the issue.

On Windows 8 or 10, you need to remove "C:\ProgramFile\Oracle\Java\javapath" from path in environment variable.

This may resolve the issue.

Isuru Sandamal

Posted 2011-01-25T19:07:24.723

Reputation: 1

2This has been suggested already, 2 years ago – Ramhound – 2018-01-28T03:43:27.747