如何设置Homebrew安装的Java路径? [英] How to set the Java path installed by Homebrew?

查看:1339
本文介绍了如何设置Homebrew安装的Java路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 9安装在我的MacBook(OS X 10.11 El Capitan)中。因为我需要Java 8,所以我使用Homebrew安装它。

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

但是,终端中的Java版本仍为9。

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) 

当前Java的安装位置似乎是在 /系统/库/框架/.../命令

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/

当然我可以将上述路径添加到 PATH 〜/ .profile 中的环境变量,但我想知道是否有更健壮的方法来设置旧Java版本的路径。

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.

推荐答案

我使用 Maarten Mulders

我将以下内容添加到我的bash配置文件中(文件我的主目录中的 .bash_profile

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"

当我想要更改为Java版本时,我只需执行 j7 在终端中。

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

这篇关于如何设置Homebrew安装的Java路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆