在 Mac 上理解 Oracle 的 Java

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

Understanding Oracle's Java on Mac

javamacosoracle

提问by rjcarr

I've been using Java on OS X for many, many years and recently when Apple stopped including Java by default I let the OS go and install it for me (Apple's variety, of course).

我已经在 OS X 上使用 Java 很多年了,最近当 Apple 停止默认包含 Java 时,我让 OS 为我安装它(当然是 Apple 的品种)。

So now I'm using OS X 10.8 and I need to install Java 7 so I just got Oracle's Update 15 in DMG form and ran the installer. It updated my /usr/bin/java (and related files) to point here:

所以现在我使用的是 OS X 10.8,我需要安装 Java 7,所以我刚刚获得了 DMG 形式的 Oracle 更新 15 并运行了安装程序。它更新了我的 /usr/bin/java(和相关文件)以指向这里:

/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

Tracing this back to '/System/Library/Frameworks/JavaVM.framework/Versions' everything either points to 'Current' or 'CurrentJDK', the former being a link to 'A' (which is Oracle's Java 7, from what I can tell, not sure why it is 'A') and the latter being a link to Apple's Java 6 in '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk'.

将此追溯到“/System/Library/Frameworks/JavaVM.framework/Versions”,一切都指向“Current”或“CurrentJDK”,前者是指向“A”的链接(据我所知,这是 Oracle 的 Java 7)告诉,不知道为什么它是“A”),后者是“/System/Library/Java/JavaVirtualMachines/1.6.0.jdk”中指向 Apple 的 Java 6 的链接。

Now this is all really confusing but this isn't even my question yet. It appears there is a Java 7 installed here:

现在这一切都非常令人困惑,但这甚至还不是我的问题。这里似乎安装了 Java 7:

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

But there is also a Java 7 installed here:

但是这里也安装了 Java 7:

/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk

Finding 'java' in both and printing out the version yields the same version and build (java version "1.7.0_15"), however, when hashing the files they are different.

在两者中找到 'java' 并打印出版本会产生相同的版本和构建(java 版本“1.7.0_15”),但是,当对文件进行散列时,它们是不同的。

So does this mean Oracle installed Java 7 in two different places? If so, why? Which am I supposed to use? And why do some things still point to Java 6 (CurrentJDK).

那么这是否意味着 Oracle 在两个不同的地方安装了 Java 7?如果是这样,为什么?我应该使用哪个?以及为什么有些事情仍然指向 Java 6 (CurrentJDK)。

I've looked on Oracle's website but nothing there clears anything up.

我查看了 Oracle 的网站,但没有任何内容可以解决。

回答by bdash

Oracle's JVM is only installed in one location. You've been misled!

Oracle 的 JVM 仅安装在一个位置。你被误导了!

As you've noted, the Java commands in /usr/binare symlinks to binaries in /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands. The binaries within that directory are stub applications that determine which Java VM to use*, and then exec the corresponding real binary within that VM version. This is why all of the binaries within /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commandsare almost identical in size, despite the fact that you'd expect them to be implementing quite different functionality.

正如您所指出的,中的 Java 命令/usr/bin/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands. 该目录中的二进制文件是确定要使用哪个 Java VM* 的存根应用程序,然后在该 VM 版本中执行相应的实际二进制文件。这就是为什么其中的所有二进制文件的/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands大小几乎相同的原因,尽管您希望它们实现完全不同的功能。

You can see this in action by using dtrace:

您可以使用dtrace以下命令查看此操作:

mrowe@angara:~$ sudo dtrace -n 'syscall::posix_spawn:entry { trace(copyinstr(arg1)); }' -c "/usr/bin/java -version"
dtrace: description 'syscall::posix_spawn:entry ' matched 1 probe
dtrace: pid 44727 has exited
CPU     ID                    FUNCTION:NAME
  8    619                posix_spawn:entry   /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java

The given dtraceinvocation prints out the path argument to posix_spawnwhen it is called by java -version. In my case the stub application has found Apple's Java 1.6 runtime in /System/Library/Java/JavaVirtualMachines/1.6.0.jdkand is invoking that version of the javacommand.

给定dtrace调用打印出的路径参数posix_spawn时,它被称为java -version。就我而言,存根应用程序已在其中找到 Apple 的 Java 1.6 运行时,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk并且正在调用该版本的java命令。

The stub binaries also have another benefit: when they detect that no Java VM is installed they will prompt the user to install one.

存根二进制文件还有另一个好处:当它们检测到没有安装 Java VM 时,它们会提示用户安装一个。

As for the CurrentJDKsymlink, as best as I can tell this for sake of backwards-compatibility with the past when Apple was the only source of the JVM on OS X.

至于CurrentJDK符号链接,为了与过去的向后兼容,当 Apple 是 OS X 上 JVM 的唯一来源时,我尽我所能说出这一点。



* A combination of factors are considered when determining which Java VM should be used. JAVA_HOMEis used if set (try JAVA_HOME=/tmp java). If JAVA_HOMEis not set then the list of all virtual machines on the system is discovered. The JAVA_VERSIONand JAVA_ARCHenvironment variables are used, if set, to filter the list of virtual machines to a particular version and supported architecture. The resulting list is then sorted by architecture (preferring 64-bit over 32-bit) and version (newer is better), and the best match is returned.

* 在确定应使用哪个 Java VM 时会考虑多种因素。JAVA_HOME如果设置(尝试JAVA_HOME=/tmp java)则使用。如果JAVA_HOME未设置,则会发现系统上所有虚拟机的列表。该JAVA_VERSIONJAVA_ARCH环境变量的使用,如果设置,虚拟机列表中筛选到一个特定的版本和支持的体系结构。然后将结果列表按体系结构(64 位优于 32 位)和版本(新的更好)进行排序,并返回最佳匹配。

回答by Ian Roberts

The Oracle Java 7 JRE(i.e. the one that is used by the web browser plugin to run applets and Java Web Start) installs itself in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home, and it is this one that any automatic updates will affect. The JDK(the one you download from http://www.oracle.com/technetwork/java/javase/downloads/index.html) installs by creating a directory under /Library/Java/JavaVirtualMachines, and it's up to you to update this yourself. You can have multiple JDK versions installed side by side but only one "public" JRE under JavaAppletPlugin.plugin(which will correspond to the latest installed JDK or a later version if it has been auto-updated since).

Oracle Java 7 JRE(即 Web 浏览器插件用于运行小程序和 Java Web Start 的JRE)将自身安装在 中/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home,并且任何自动更新都会影响该JRE。在JDK(你下载一个http://www.oracle.com/technetwork/java/javase/downloads/index.html)安装通过创建下的一个目录/Library/Java/JavaVirtualMachines,它是由你来此更新自己。您可以并排安装多个 JDK 版本,但只能安装一个“公共”JRE JavaAppletPlugin.plugin(这将对应于最新安装的 JDK 或更高版本,如果它已自动更新)。

As explained by bdash, the commands under /usr/binare stubs that delegate to whichever JDK/JRE is pointed to by the JAVA_HOMEenvironment variable, or if that is not set then they will pick the most appropriate Java to run. You can use /usr/libexec/java_hometo see which one the stubs would pick. If noJava is installed the stubs will offer to install the latest Apple Java 6 (as far as I know they will notoffer to install Java 7).

正如 bdash 所解释的,下面的命令/usr/bin是委托给JAVA_HOME环境变量指向的任何 JDK/JRE 的存根,或者如果没有设置,那么他们将选择最合适的 Java 来运行。您可以使用/usr/libexec/java_home来查看存根会选择哪一个。如果安装 Java,存根将提供安装最新的 Apple Java 6(据我所知,它们不会提供安装 Java 7)。

回答by Albert Sun

I find this post: https://developer.apple.com/library/mac/qa/qa1170/_index.htmlThe /usr/libexec/java_home tool dynamically finds the top Java version specified in Java Preferences for the current user.

我找到了这篇文章:https: //developer.apple.com/library/mac/qa/qa1170/_index.html/usr/libexec/java_home 工具为当前用户动态查找 Java 首选项中指定的顶级 Java 版本。