brew java 切换_如何设置Homebrew安装的Java路径?

Java 9 is installed in my MacBook (OS X 10.11 El Capitan). As I needed Java 8, I've installed it using Homebrew.

$ brew cask install java8

However, the Java version is still 9 in the terminal.

$ java -version

java version "9"

Java(TM) SE Runtime Environment (build 9+181)

Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

The install location of the current Java seems to be in /System/Library/Frameworks/.../Commands

$ ls -la /usr/bin/java

lrwxr-xr-x 1 root wheel 74 Sep 23 2017 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

Homebrew seems to have installed Java 8 in

/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/

Of course I could just prepend the above path to the PATH environment variable in ~/.profile, but I wanted to know whether there is a more robust way of setting the path for the older Java version.

解决方案

I use the method suggested by Maarten Mulders.

I added the following to my bash profile (the file .bash_profile in my home directory).

alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"

alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"

alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"

When I want to change to a Java version, I simply execute j7 in the terminal.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值