java -version and javac -version showing different versions

后端 未结 9 1841
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 01:29

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

In the environment\'s path I specif

相关标签:
9条回答
  • 2020-12-03 01:30

    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.

    0 讨论(0)
  • 2020-12-03 01:41

    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.

    0 讨论(0)
  • 2020-12-03 01:45

    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.

    0 讨论(0)
  • 2020-12-03 01:48

    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

    0 讨论(0)
  • 2020-12-03 01:48

    My case (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

    0 讨论(0)
  • 2020-12-03 01:50

    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.

    0 讨论(0)
提交回复
热议问题