JDK
On Mac OS, /usr/bin/java
and friends are stubs that delegate to the real JDK commands. These stubs respect the setting of your JAVA_HOME
environment variable, but for this to work you need to install the JDK (from http://www.oracle.com/technetwork/java/javase/downloads/index.html) as opposed to the JRE (from http://java.com).
The JDK installs into /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk
(for whatever value of NN), so set your JAVA_HOME
environment variable to /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home
to make /usr/bin/java
use 1.7. You can switch back to 1.6 simply by pointing your JAVA_HOME
to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
instead. You can use the /usr/libexec/java_home
tool to find the right value automatically, for example to make /usr/bin/java
use Java 7 you can do
export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`
and to make it use Java 6 you can do
export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`
The same applies to Java 8 (using -v '1.8*'
). This will pick up the latest installed JDK for the relevant major version, you don't need to remember to change the NN
by hand when you install an update.
JRE
If you want to run the 1.7 or 1.8 JRE from the command line, it can be found in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
. This is a fixed path and you can only have one "public" JRE installed at any given time.
$ /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
You could use a shell alias in your .bashrc
alias java_jre='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…