java version "1.6.0_65"Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
**USAGE**
/usr/libexec/java_home helps users set a $JAVA_HOME in their login rc files, or provides a way forcommand-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-tecture requirement. The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,which is useful for starting Java command-line tools from launchd plists without hardcoding the fullpath to the Java command-line tool.
Usage for bash-style shells:$ export JAVA_HOME=`/usr/libexec/java_home`
Usage for csh-style shells:% setenv JAVA_HOME `/usr/libexec/java_home`
#list available jdksalias jdks="/usr/libexec/java_home -V"# jdk version switching - e.g. `jdk 6` will switch to version 1.6function jdk() {echo "Switching java version";export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;java -version;}
(*)当前的答案要么已经过时(不再适用于macOS El Capitan或Sierra),要么只解决单个JDK,或者不解决系统范围的问题。许多人解释了如何更改$JAVA_HOME,但这只影响当前的shell和从那里启动的内容。它不会影响从操作系统启动器启动的应用程序(除非你更改了正确的文件并注销/登录,这很乏味)。jenv也是如此,它很酷,但据我所知,它只是更改环境变量,所以它有同样的限制。
$ cat ~/.jdk#!/bin/bash
#list available jdksalias jdks="/usr/libexec/java_home -V"# jdk version switching - e.g. `jdk 6` will switch to version 1.6function jdk() {echo "Switching java version $1";
requestedVersion=$1oldStyleVersion=8# Set the versionif [ $requestedVersion -gt $oldStyleVersion ]; thenexport JAVA_HOME=$(/usr/libexec/java_home -v $1);elseexport JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;fi
echo "Setting JAVA_HOME=$JAVA_HOME"
which javajava -version;}
切换到Java8
$ jdk 8Switching java version 8Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/usr/bin/javajava 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)
切换到Java9
$ jdk 9Switching java version 9Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/usr/bin/javajava version "9.0.1"Java(TM) SE Runtime Environment (build 9.0.1+11)Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. source ~/.zshrc - Restart zshrc to reflect the changes.
3. echo $JAVA_HOME - make sure path is set up properly----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
4. java -version
--> java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
在适当的位置正确解压并重命名。 e. g/mnt/star/program/java/jdk-1.8
做一个软链接,稍后会改为切换java版本。 e. gln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java 因此/mnt/star/program/java/java是软链接。
在启动脚本中设置JAVA_HOME。 可以使用像/etc/profile.d/eric.sh这样的文件,或者只使用~/.bashrc。 e. gJAVA_HOME=/mnt/star/program/java/java
然后打开一个新的bash shell。java -version应该打印java版本。
[更多版本-部分]
根据需要下载并安装更多Java版本,类似于上述步骤。 e. g /mnt/star/program/java/jdk-11
[开关-部分]
在~/.bashrc中,为各种Java版本定义变量。 e. g _E_JAVA_HOME_11='/mnt/star/program/java/jdk-11' _E_JAVA_HOME_8='/mnt/star/program/java/jdk-8' # dir of default version, _E_JAVA_HOME_D=$_E_JAVA_HOME_8
在~/.bashrc中,定义命令切换Java版本。 ## switch java version, alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME" alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME" # default java version, alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME" alias jv="java -version"
在终端中,source ~/.bashrc使更改生效。
然后可以使用定义的命令进行切换。
命令-从上面配置
命令:
jv11 切换到Java11
jv8 切换到Java8
jvd 切换到默认Java版本,由上面定义的_E_JAVA_HOME_D表示。
jv 显示java版本。
输出示例:
eric@eric-pc:~$ jvjava version "1.8.0_191"Java(TM) SE Runtime Environment (build 1.8.0_191-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric@eric-pc:~$ jv11eric@eric-pc:~$ jvjava version "11.0.1" 2018-10-16 LTSJava(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
eric@eric-pc:~$ jvderic@eric-pc:~$ jvjava version "1.8.0_191"Java(TM) SE Runtime Environment (build 1.8.0_191-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric@eric-pc:~$
机制
它通过更改软链接进行切换,该软链接用作JAVA_HOME。
Tips
在我的机器上手动安装jdk时,我保留次要版本,然后与主要版本建立软链接,但没有次要版本。 e. g //这是实际的目录, jdk1.8.0_191
# set and change java versionsfunction java-change() {echo "----- old java version -----"java -versionif [ $# -ne 0 ]; thenexport JAVA_HOME=`/usr/libexec/java_home -v $@`export PATH=$JAVA_HOME/bin:$PATHfiecho "----- new java version -----"java -version}