java -version 和 javac -version 显示不同的版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25074017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:33:59  来源:igfitidea点击:

java -version and javac -version showing different versions

java

提问by SparkOn

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

我已经java 7java 8都安装在我的 Windows 系统下C:\Program Files\Java

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

在环境的路径中,我指定了这样的路径java 7

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

%JAVA_HOME%/bin在哪里 JAVA_HOME = C:\Program Files\Java\jdk1.7.0_17

But when I type javac -versionit shows javac 1.7.0_17which makes sense

但是当我输入时,javac -version它会显示javac 1.7.0_17这是有道理的

but when I do java -versionit shows

但是当我这样做时 java -version它显示

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?

知道这里可能有什么问题吗?

EditThe installed folder : enter image description here

编辑安装的文件夹: 在此处输入图片说明

And one more thing echo %PATH%has only one occurence of JDKthat is for java 7and no occurence for JREfound in it.

并且另外一件事echo %PATH%只有一次出现JDKjava 7并且没有出现JRE在其中。

回答by nobalG

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

我想,当你正在编写javac,它正从价值jre7这是目前正在使用您为发展目的,当你写的java version,是越来越的价值versionjre8目前正在使用的安装的应用程序在系统上,因为JREjre7您用于开发的系统之前提到了您的系统

回答by Mustafa sabir

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

因为你有一个问题是发生最有可能JRE在你的path,你的面前JDK。当我们在 JDK 中添加 JDKpath并且不删除之前添加的 JRE时,通常会发生这种情况。因此,当您执行java它时指向 JRE,当您执行javac它时指向另一个版本/JDK(因为 jre 中没有 javac)

Edit:- Since you edited your question and posted not having any JREs in your path, You can copy your %JAVA_HOME%and paste it first in your path variable (make sure its before system32 path), I have faced this issue before (while installing android sdk) and doing this resolved it.

编辑:- 由于您编辑了您的问题并发布了您的路径中没有任何 JRE,您可以复制您的%JAVA_HOME%并将其首先粘贴到您的路径变量中(确保它在 system32 路径之前),我之前遇到过这个问题(在安装 android sdk 时) ) 并这样做解决了它。

Also found a possible explanation for this from here:-

还从这里找到了对此的可能解释:-

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.

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.

回答by prashant thakre

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

要解决此类环境问题,我始终相信命令 whereis,在 windows 下载whereis.exe中执行相同操作,然后在路径中设置并执行命令

whereis java.exe

whereis javac.exe

哪里有java.exe

javac.exe在哪里

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

您将很容易找到在环境中调用 java 的位置以及 javac 的确切路径。

回答by TheConstructor

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.

好的,正如您所说,您的路径上没有 JRE,我假​​设where java(如果您有 Windows 7)会给您C:\Windows\System32\java.exe.

Changing the javarunning

改变java运行

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 -versionis returning. If this does not work, you will probably need to reinstall the Java version of which you want to run the javacommand, 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).

尝试使用控制面板中的 Java 设置对话框更改当前处于活动状态的 Java 系统版本。这应该改变java -version正在返回的版本。如果这不起作用,您可能需要重新安装要运行java命令的 Java 版本,明确指定路径,编写自己的包装器(如果您与包装器位于同一目录中,则可以使用)或放置JRE 路径之前C:\Windows\System32\进入您的路径(不知道最后一个选项是否真的是一个好选项)。

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 PATHbefore C:\Windows\System32\java.exeor you could name it java7.batand put it anywhere on you path to be able to run the Java 7 JRE if you desire to do so.

并且你可以命名它java.bat(如果你在同一个目录中它会在哪里工作,或者把它放在PATH之前,C:\Windows\System32\java.exe或者你可以命名它java7.bat并将它放在你路径上的任何地方,以便能够运行 Java 7 JRE,如果你想这样做的话.

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

如果您想更改JAVA_HOME运行时间,包装器也是一个不错的选择。

Changing the javacrunning

改变javac运行

If you want to run a different javachit Windows + Pauseand open the system dialog to change the PATHso 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.

如果您想运行不同的javac点击Windows + Pause并打开系统对话框进行更改,PATH使其包含 JDK 8 而不是 JDK 7 的路径。您将需要重新启动命令行以显示更改。

回答by Igor Soudakevitch

Yes, it istrue that in order to deal with mismatching versions we need to modify the PATH variable, and the wherecommand (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,

是的,这事实,为了对付不匹配的版本中,我们需要修改PATH变量,where命令(至少在Windows)是非常方便的找出哪些程序被第一次拿起。但是,有一个转折点:例如,当您在运行后查看 PATH var 的内容时,

echo %path% > path.txt

you should be aware of the fact that PATH is actually composed of TWO sections: one is System PATHand the other is Current User PATH, and it is System Paththat 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.

您应该知道 PATH 实际上由两个部分组成:一个是System PATH另一个是Current User PATH,它是System Path首先评估但最后打印。因此,只要在System PATH 的某处有另一个旧版本的地方,简单地在所有内容前面放置一条指向您的全新 JDK 的路径将是无用的。

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 PATHsection to make these entries the last ones within the search queue.

在您的系统路径中。只需将它们移动到当前用户路径部分的尾部,使这些条目成为搜索队列中的最后一个。

回答by bortunac

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

我的情况(移动 jdk8 -> jdk9 -> jdk11)我在 PATH 中有 jdk8 留下的垃圾(在 JAVA_HOME:"C:\path\java11" 之前)

so I just removeC:\ProgramData\Oracle\Java\javapath_jdk8from system variables

所以我只是 从系统变量中删除C:\ProgramData\Oracle\Java\javapath_jdk8

// or place it below javapath_jdk11

// 或者把它放在 javapath_jdk11 下面

回答by Dhananjay Yelwande

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.

转到 Windows 机器中的环境变量。在用户变量中:确保将 - 您的用户变量“JAVA_HOME”值设置为“C:\Program Files\Java\jdk-xxxx\bin”,其中“jdk-xxx”是您的 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.

在系统变量中: - 将相同的“C:\Program Files\Java\jdk-xxxx\bin”值添加到“Path”变量。确保将附加值移至所有值之上。

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

现在尝试运行 java -version 和 javac -version。为我工作。:) 这里我链接了我的截图