51

I have java 7 and java 8 both installed on my windows system under C:\Program Files\Java

In the environment's path I specified the path of java 7 like this.

%JAVA_HOME%/bin where JAVA_HOME = C:\Program Files\Java\jdk1.7.0_17

But when I type javac -version it shows javac 1.7.0_17 which makes sense

but when I do java -version it shows

java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode) 

enter image description here

Any idea what may be the problem here?

Edit The installed folder : enter image description here

And one more thing echo %PATH% has only one occurence of JDK that is for java 7 and no occurence for JRE found in it.

9
  • 3
    Are you sure you installed the Java 8 JDK, and not just its JRE? Commented Aug 1, 2014 at 6:21
  • 2
    See if echo %PATH% contains the JRE folder of Java 8 before the JDK folder of Java 7 Commented Aug 1, 2014 at 6:21
  • set your jre7 bin location path at first like - set PATH=%JAVA%\bin;%PATH% so it will pick your jdk7 bin path first. Commented Aug 1, 2014 at 6:29
  • @Mureinik please if you dont mind check the edit of the question Commented Aug 1, 2014 at 6:29
  • @ChrisMartin if you read the query properly you can most probably find what is the problem Commented Aug 1, 2014 at 6:32

13 Answers 13

41

The issue is occuring most probably because you have a JRE in your PATH, before your JDK. This usually happens when we add JDK in the PATH and do not remove previously added JREs. Thus when you do java it points to the JRE and when you do javac it points to the other version/JDK (since the JRE does not have a javac in it)

If you don't have any JREs in your path, then you can copy your %JAVA_HOME% and paste it first in your path variable (make sure its before the system32 path).

A possible reason for why this works is explained in this answer:

JRE also puts a java.exe to c:\Windows\System32, that's how first command is resolved. Second command is resolved by the C:\Program Files\Java\jdk1.7.0_02\bin entry in your PATH variable. If (and when) you are developing from the command prompt, you have to adjust the PATH variable so that C:\Program Files\Java\jdk1.7.0_02\bin is before c:\Windows\System32.

Sign up to request clarification or add additional context in comments.

5 Comments

echo %PATH% shows only one occurence of JDK and that is for java 7 and no occurence for JRE found in it.
Ok so I just found that c:\Windows\System32\java.exe date-modified is same as the java 8 installation date so I guess that the java.exe in c:\Windows\System32 is of java8 how to resolve this problem?
Yes, which i guess confirms that its taking java.exe of java 8 from system32, I have written in my answer, You can copy your %JAVA_HOME% and paste it first in your path variable (make sure its before system32 path) , this way it will get both jdk and jre of java 7 before system32 version that is of java 8
+1 not an ideal solution though but indeed thanks for the help :)
Be notice USER path will append to SYSTEM path. However the C:\Windows\System32 locates in SYSTEM path, you have to add %JAVA_HOME%\bin into it which will pollute my SYSTEM path. GUI installer sucks.
10

Go to Environment Variables in your windows machine. In User Variables : Make sure to set - Your user variable "JAVA_HOME" value to "C:\Program Files\Java\jdk-xxxx\bin" where "jdk-xxx" is the version of your jdk.

In System Variable : - Add same "C:\Program Files\Java\jdk-xxxx\bin" value to "Path" variable. make sure to move the added value on top of all values.

Now try running java -version and javac -version. worked for me. :) Here i've linked my screenshot

Comments

7

It's all about $JAVA_HOME system variable

In my case

for Windows
(moving jdk8 -> jdk9 -> jdk11) I had a garbage left by jdk8 in PATH (before JAVA_HOME:"C:\path\java11")

so I just remove C:\ProgramData\Oracle\Java\javapath_jdk8 from system variables

// or place it below javapath_jdk11

for MAC
edit .zshrc to have

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-???.jdk/Contents/Home" 

Comments

6

Ok, as you said that you have no JRE on your path, I assume that where java (if you have Windows 7) will give you C:\Windows\System32\java.exe.

Changing the java running

Try using the Java-Settings Dialog from the control panel to change the Java System Version, which is currently active. This should change the version java -version is returning. If this does not work, you will probably need to reinstall the Java version of which you want to run the java command, specify the path explicitly, write you own wrapper (which works if you are in the same directory as the wrapper) or place the JRE path before C:\Windows\System32\ onto your path (don't know if last option really is a good one).

A wrapper could look just like this:

@"C:\Program Files\Java\jre7\bin\java.exe" %* 

and you can either name it java.bat (where it will work if you are in the same directory or put it in PATH before C:\Windows\System32\java.exe or you could name it java7.bat and put it anywhere on you path to be able to run the Java 7 JRE if you desire to do so.

A wrapper is also a good option if you want to change the JAVA_HOME when running.

Changing the javac running

If you want to run a different javac hit Windows + Pause and open the system dialog to change the PATH so it contains the path to your JDK 8 instead of your JDK 7. You will need to restart your command line for changes to show.

5 Comments

I just found that c:\Windows\System32\java.exe date-modified is same as the java 8 installation date so I guess that the java.exe in c:\Windows\System32 is of java8 how to resolve this problem without tempering the path?
@M.Sharma Second paragraph of my answer. Guess reinstalling the JRE 7 could be the best option. For me there is now way to change the System JRe through the Java-settings-panel also I think that this was once possible.
java path sometimes really can be a pain in the ass
So that means next time if I uninstall and reinstall java 8 again I'll face the same problem unless I keep my desired path before c:\Windows\System32
@M.Sharma that would probably be the case. Maybe you really should consider using full path or a wrapper.
3

To resolve such kind of environment issue I always believe on command whereis, To do the same in windows download whereis.exe then set in path and execute the command

whereis java.exe

whereis javac.exe

You will easily find the exact path from which location java is being called in environment as well about javac.

2 Comments

Yes but in other OS version its not there so those developers can use it easily.
Where you could also try for %i in (java.exe) do @echo %~$PATH:i which is probably not as fancy, but does not require you to add new programs to your system.
2

Please check that you have replaced "C:\ProgramData\Oracle\Java\javapath;" with %JAVA_HOME%/bin in the PATH variable. This did the trick for me.

Comments

1

A side note with the accepted answer. I couldn't leave the trail of java-1.8.0, but I have installed java 17 and want java "17", so, I placed java 17 bin path in the first place (top most ) of path variables, Home variable and user variable, after that when I type in cmd : java -version, it also show java-1.8.0, then I restart cmd, restart Intellij ide, then I type java -version, then it shows :

java version "17" 2021-09-14 LTS Java(TM) SE Runtime Environment (build 17+35-LTS-2724) Java HotSpot(TM) 64-Bit Server VM (build 17+35-LTS-2724, mixed mode, sharing) 

and when I type :

C:\Users\dell>javac -version 

It shows :

javac 17 

Here is My path variables ScreenShots and command window :

enter image description here

enter image description here

enter image description here

Comments

1

I have a similar case but both command javac --version show the old version of sdk. It shows "javac 11.0.8" even it should be "javac 19" which is the new version just be installed.

  1. Go to search bar > type > env
  2. click menu "Edit the system environment variables"
  3. Advanced tab > click on button "Environment Variables..."
  4. Under "System Variable" > select "Path" > click "Edit"
  5. Looking for Java path such as "C:\Program Files\Java\jdk-19\bin"
  6. Move the path using "Move Up" or "Move Down" button to the position before "%SystemRoot%\system32" Path
  7. OK > OK

Try the command agian. Hope this may help you guys.

Comments

1

FOR LINUX USERS

if java and javac are showing different versions, then you can easily set them to the correct or same versions

step 1

sudo update-alternatives --config javac 

output :

 Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode 

this will show the following message:

start(*) indicates the javac version that your system is using. You can change it by selecting your required version.

step 2

sudo update-alternatives --config java 

output :

 Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 

Now again you can select the version you want to run for java.

This way you can ensure that java and javac are having the same versions.

Comments

0

Yes, it is true that in order to deal with mismatching versions we need to modify the PATH variable, and the where command (at least, in Windows) is very handy in finding out which program gets picked first. There's, however, a twist: when you look at the contents of yout PATH var after running, for example,

echo %path% > path.txt 

you should be aware of the fact that PATH is actually composed of TWO sections: one is System PATH and the other is Current User PATH, and it is System Path that gets evaluated first but printed last. So simply placing a path to your, say, brand-new JDK in front of everything will be useless as long as there's another place with older version somewhere in the System PATH.

This particular problem is especially common when you have entries such as

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath 

in your System PATH. Simply move them to the tail of your Current User PATH section to make these entries the last ones within the search queue.

Comments

0

Check your JAVA_HOME variable under environment variables in Windows machine. Make sure It is pointing to the version that you want. Next check java jdk path added into your "path" environment variable. Make sure It is the version that you want, Otherwise change this to your JDK version path and then move this to the top of the list of values in path variable. This should mark your desired version as the first choice and all issues will be resolved. I did so, and everything is good.

Comments

0

I found my javac was pointed to a specific location called 'Eclipse Adoption' in my AppData directory within my 'User' folder. I didn't realize that it was in my PATH variable, so I simply removed it, and the problem was solved for me

Comments

-2

I guess when you are writing javac, it is getting the value from jre7 which is currently being used by you for development purposes and when you write java version, it is getting the value of the version from the jre8 which is currently being used by the installed applications on your system because your JRE of the system is mentioned before the jre7 which you are using for development

2 Comments

This is a comment than an answer.
@RuchiraGayanRanaweera OP's question is not about some error in code,its about explaining some fact

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.