如何更改从/usr/libexec/java_home返回的Mac OS的默认Java VM [英] How can I change Mac OS's default Java VM returned from /usr/libexec/java_home
问题描述
(不确定是否应该在SU上进行...迁移当然是一种选择,但是更多的程序员在这里阅读了问题,所以就去了).
(Wasn't sure if this should go on SU... migration is certainly an option, but more programmers read questions here, so here goes).
我正在运行Mac OS X 10.8.4,并且安装了Apple的JDK 1.6.0_51和Oracle的JDK 1.7.0_25.我最近为一些需要它的预发行软件安装了Oracle 1.8预览JDK.现在,当我运行/usr/libexec/java_home时,我得到了:
I am running Mac OS X 10.8.4, and I have Apple's JDK 1.6.0_51 installed as well as Oracle's JDK 1.7.0_25. I recently installed Oracle's 1.8 preview JDK for some pre-release software that requires it. Now, when I run /usr/libexec/java_home, I get this:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
1.7.0_25, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
1.6.0_51-b11-457, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_51-b11-457, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
太好了.
但是,正在运行:
$ java -version
返回:
java version "1.8.0-ea"
这意味着Java的默认版本当前是预发行版本,它破坏了一些正常"程序包(在我的情况下为VisualVM).
That means that the default version of Java is currently the pre-release version, which breaks some "normal" packages (in my case, VisualVM).
我无法设置JAVA_HOME
,因为即使从命令行启动(例如$ open /Applications/VisualVM.app
),启动应用程序也会忽略环境变量.
I can't set JAVA_HOME
because launching applications ignores environment variables, even when launching from the command line (e.g. $ open /Applications/VisualVM.app
).
那么,是否可以在 global 全局设置JVM顺序首选项的地方编辑文件?
So, is there a file I can edit where I can set my JVM ordering preferences globally?
(请不要告诉我启动Java首选项面板,因为那根本行不通:它不包含任何有用的东西,仅列出了我已安装的4个JVM之一.)
(Please don't tell me to launch the Java Preferences Panel because that simply does not work: it does not contain anything useful and only lists one of the 4 JVMs that I have installed.)
更新:
Oracle JVM位于/Library/Java/JavaVirtualMachines
中.将JDK 1.8目录重命名为jdk1.8.0.jvm.xyz
不会更改任何内容:java_home
仍会在正确的位置找到它,并且运行/usr/bin/java仍会执行1.8 JVM.这不是synlinks等的问题.
Oracle JVMs live in /Library/Java/JavaVirtualMachines
. Re-naming the JDK 1.8 directory to jdk1.8.0.jvm.xyz
does not change anything: java_home
still finds it in the right place, and running /usr/bin/java still executes the 1.8 JVM. This is not an issue with synlinks, etc.
类似问题的答案
虽然此答案提供了相当于可以删除java_home所提取的Java版本的hack信息,但它仍然无法回答如何 java_home选择其默认值以及用户是否可以无损设置它的问题.
While this answer offers what amounts to a hack that will remove versions of Java from being picked up by java_home, it still does not answer this question of how java_home chooses its default and whether or not users can non-destructively set it.
推荐答案
我认为JAVA_HOME
是您可以做的最好的事情.诸如java
和javac
之类的命令行工具将尊重该环境变量,您可以使用/usr/libexec/java_home -v '1.7*'
为您提供一个合适的值放入JAVA_HOME
中,以使命令行工具使用Java 7.>
I think JAVA_HOME
is the best you can do. The command-line tools like java
and javac
will respect that environment variable, you can use /usr/libexec/java_home -v '1.7*'
to give you a suitable value to put into JAVA_HOME
in order to make command line tools use Java 7.
export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"
但是标准的可双击应用程序捆绑包根本不使用/Library/Java
下安装的JDK.使用Apple JavaApplicationStub
的旧式.app
捆绑包将使用/System/Library/Frameworks
中的Apple Java 6,而使用
But standard double-clickable application bundles don't use JDKs installed under /Library/Java
at all. Old-style .app
bundles using Apple's JavaApplicationStub
will use Apple Java 6 from /System/Library/Frameworks
, and new-style ones built with AppBundler without a bundled JRE will use the "public" JRE in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
- that's hard-coded in the stub code and can't be changed, and you can't have two different public JREs installed at the same time.
I've had a look at VisualVM specifically, assuming you're using the "application bundle" version from the download page, and this particular app is not an AppBundler application, instead its main executable is a shell script that calls a number of other shell scripts and reads various configuration files. It defaults to picking the newest JDK from /Library/Java
as long as that is 7u10 or later, or uses Java 6 if your Java 7 installation is update 9 or earlier. But unravelling the logic in the shell scripts it looks to me like you can specify a particular JDK using a configuration file.
创建一个包含该行的文本文件~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf
(用您使用的VisualVM的任何版本替换1.3.6)
Create a text file ~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf
(replace 1.3.6 with whatever version of VisualVM you're using) containing the line
visualvm_jdkhome="`/usr/libexec/java_home -v '1.7*'`"
,这将迫使它选择Java 7而不是8.
and this will force it to choose Java 7 instead of 8.
这篇关于如何更改从/usr/libexec/java_home返回的Mac OS的默认Java VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!