升级到Java8后,javac还是显示1.7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23202516/
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-13 21:18:52  来源:igfitidea点击:

After upgrading to Java8, javac still shows 1.7

javamacosjava-8

提问by Marty Pitt

I'm having issues upgrading from JDK 1.7 -> 1.8 on OSX. The upgrade has completed, but javac still returns 1.7 as the version.

我在 OSX 上从 JDK 1.7 -> 1.8 升级时遇到问题。升级已完成,但 javac 仍返回 1.7 作为版本。

I've downloaded JDK 8_u5from Oracle's homepage, and run the installer.

我已经从 Oracle 的主页下载了JDK 8_u5,并运行了安装程序。

I've also taken the following steps, post-install:

我还采取了以下步骤,安装后:

> export JAVA_HOME=`/usr/libexec/java_home -v 1.8`  (Executed in my .bashrc file)

> echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

> javac -version
javac 1.7.0_21

> $JAVA_HOME/bin/javac -version
javac 1.7.0_21

> $JAVA_HOME/bin/java -version    
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

I've gone through and removed Java 1.7 (and all other JDK versions), and then re-run the installer:

我已经删除了 Java 1.7(以及所有其他 JDK 版本),然后重新运行安装程序:

> ls /Library/Java/JavaVirtualMachines
jdk1.8.0_05.jdk

Still no use, javac reports the version as 1.7.0_21

还是没用,javac报告版本为1.7.0_21

> which javac
/usr/bin/javac

> ls -ltra /usr/bin/javac 
[snipped] /usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac

Within that path, Currentis a symlink to A. The contents of A/Commandsare a series of files (not symlinks).

在该路径中,Current是指向A. 的内容A/Commands是一系列文件(不是符号链接)。

> cd A/Commands
> ./javac -version
javac 1.7.0_21

> ./java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

Edit

编辑

Further to the original post, I've done some digging with jenv, as suggested on this answer.

继原帖之后,我对jenv进行了一些挖掘,如this answer中所建议的那样。

> jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home                 
> jenv global oracle64-1.8.0.05
> jenv version
oracle64-1.8.0.05 (set by /Users/martypitt/.jenv/version)

> jenv info java 
Jenv will exec : /Users/martypitt/.jenv/versions/oracle64-1.8.0.05/bin/java

> jenv info javac
Jenv will exec : /Users/martypitt/.jenv/versions/oracle64-1.8.0.05/bin/javac

> javac -version
javac 1.7.0_21

This casts dispersions on my thoughts that this was a random javac lurking in my path, which was somehow getting invoked.

这使我的想法分散,即这是一个潜伏在我路径中的随机 javac,不知何故被调用。

To be sure, I've nuked my Java completely, and tried again:

可以肯定的是,我已经完全取消了我的 Java,然后再试了一次:

> cd /Library/Java/JavaVirtualMachines
> ls 
jdk1.7.0_55.jdk 
jdk1.8.0_05.jdk
> sudo rm -rf *  
> ls
<<empty>>
> java -version
java version "1.6.0_65"
> javac -version
javac 1.6.0_65
> which javac
/usr/bin/javac

I then re-downloaded a fresh copy the installerand ran it.

然后我重新下载了安装程序新副本并运行它。

> java -version
java version "1.8.0_05"
> javac -version
javac 1.7.0_21

Update

更新

I tried removing all JDK's, XCode and all developer tools, and re-installed fresh. Same results.

我尝试删除所有 JDK、XCode 和所有开发人员工具,然后重新安装。结果一样。

However, I'm still at a loss -- where do I go from here? How do I get javac 1.8 to get installed?

然而,我仍然不知所措——我该去哪里呢?如何安装 javac 1.8?

采纳答案by Anthony Accioly

If /usr/libexec/java_home -v 1.8.0_05 --exec javac -versionreturns the correct version, then your problem is with:

如果/usr/libexec/java_home -v 1.8.0_05 --exec javac -version返回正确的版本,那么您的问题在于:

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

With a privileged user execute:

使用特权用户执行:

cd /System/Library/Frameworks/JavaVM.framework/Versions/
rm CurrentJDK
ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/ CurrentJDK

Solution found inMankeh Blog

Mankeh 博客中找到的解决方案



Also check this answeron Super User for dynamically switching JDK versions.

还可以在超级用户上查看此答案以动态切换 JDK 版本。



Update:I guess I've found the culprit!

更新:我想我找到了罪魁祸首!

Try this:

尝试这个:

rm -rf ~/Library/Java/Extensions
sudo rm -rf /Library/Java/Extensions

Solution found in:Java 1.7 on OSX 10.9.2 running as 1.5?

解决方案:Java 1.7 on OSX 10.9.2 running as 1.5?

回答by skiwi

This is (most likely) a bug in the installation process of jre8.

这(很可能)是jre8安装过程中的一个错误。

I am getting the exact same behaviour when navigating to my jre8folder, which is the one which Windows (in my case) shows by default:

导航到我的jre8文件夹时,我得到了完全相同的行为,这是 Windows(在我的情况下)默认显示的:

  • java version: 1.8.0, runtime environment 1.8.0-b132, hotspot 64-bit JVM 25.0-b70
  • javac version: 1.7.0_25
  • java版本:1.8.0,运行环境1.8.0-b132,热点64位JVM 25.0-b70
  • javac 版本:1.7.0_25

When navigating to the jdk8directory, it shows the following:

导航到jdk8目录时,它显示以下内容:

  • java version: 1.8.0, runtime environment 1.8.0-b132, hotspot 64-bit JVM 25.0-b70
  • javac version: 1.8.0
  • java版本:1.8.0,运行环境1.8.0-b132,热点64位JVM 25.0-b70
  • javac 版本:1.8.0

I did just now notice though that the jredoes never ship an javac, so this is an issue in the reference settings in the operating system then I guess.

我刚刚注意到jre从不提供javac,所以我猜这是操作系统中参考设置中的一个问题。

To fix it, you would need to manually fix the javacreference in your OS.

要修复它,您需要手动修复操作系统中的javac引用。

回答by KayG

For Windows users:

对于 Windows 用户:

It is possible, that you have to change your PATH variable. Try to set your "%JAVA_HOME%/bin" as the firstentry in the PATH.

您可能必须更改 PATH 变量。尝试将您的“%JAVA_HOME%/bin”设置为PATH 中的第一个条目。

So now it looks like this:

所以现在它看起来像这样:

 PATH    C:\Program Files\anyprogram;C:\Program Files\...;%JAVA_HOME%\bin

And you change it like this:

你像这样改变它:

 PATH    %JAVA_HOME%\bin;C:\Program Files\anyprogram;C:\Program Files\...

Reason: Any other Program initiates an other Java Version first.

原因:任何其他程序首先启动其他 Java 版本。

回答by HadYang

Maybe you can delete the standard .jar file which in your classpath. When i delete the jar in /Library/Java/Extensions, the problem will be solved. More info : Java compilation error: Mac

也许您可以删除类路径中的标准 .jar 文件。当我删除 jar 时/Library/Java/Extensions,问题将得到解决。更多信息: Java 编译错误:Mac

回答by machinat

Just adding my own experience here, I installed jdk-8u131-macosx-x64 from Oracle. Everything worked perfectly. No JAVA_HOME environment variables were present or needed.

只是在这里添加我自己的经验,我从 Oracle 安装了 jdk-8u131-macosx-x64。一切都很完美。不存在或不需要 JAVA_HOME 环境变量。

mehboob$ javac -version
javac 1.8.0_131

mehboob$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

The /System/Library showed this:

/System/Library 显示了这一点:

mehboob$ ls -l /System/Library/Frameworks/JavaVM.framework/Versions/
total 64
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Oct 17  2013 1.6.0 -> CurrentJDK
drwxr-xr-x  8 root  wheel  272 Sep  9  2014 A
lrwxr-xr-x  1 root  wheel    1 Oct 17  2013 Current -> A
lrwxr-xr-x  1 root  wheel   59 Oct 17  2013 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

While the /Library/Java had this:

虽然 /Library/Java 有这个:

mehboob$ ls -l /Library/Java/
total 8
drwxrwxr-x  2 root  admin   68 Oct 17  2013 Extensions
lrwxr-xr-x  1 root  wheel   48 Oct 17  2013 Home -> /System/Library/Frameworks/JavaVM.framework/Home
drwxr-xr-x  3 root  wheel  102 Jun  9 11:43 JavaVirtualMachines

There was nothing in Extensions, while JavaVirtualMachines only had the jdk1.8.0_131.jdk folder.

Extensions 中没有任何内容,而 JavaVirtualMachines 只有 jdk1.8.0_131.jdk 文件夹。

This shows that a default installation of JDK works properly with no extra configuration required.

这表明 JDK 的默认安装可以正常工作,无需额外配置。

回答by Huang

Firstly, you need to download and then install the jdk8(http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)

首先,您需要下载并安装jdk8(http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Secondly, do some check as follows: Does your system have a not-linked-for-shell use java 8 already? Try this in you terminal (yes, the apostrophes are important):

其次,做一些检查如下:你的系统是否已经有一个非链接的shell使用java 8?在你的终端中试试这个(是的,撇号很重要):

$ '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java' -version 

if it shows " java version "1.8.0_131" " , and then you can go to next step.

如果显示“ java version "1.8.0_131" ”,则可以进行下一步。

my screenshot

我的截图

Thirdly, edit the bash_profile:

第三,编辑 bash_profile:

$ vi ~/.bash_profile 

and type this:

并输入:

JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib
export JAVA_HOME
export CLASSPATH 

save it and quit, then

保存并退出,然后

source ~/.bash_profile 

After the above steps, and check the java version

经过以上步骤,并检查java版本

$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)