Where is JAVA_HOME on macOS Mojave (10.14) to Lion

2019-01-01 06:10发布

Java is an optional package on the latest versions of macOS.

Yet once installed it appears like the JAVA_HOME environment variable is not set properly.

标签: java macos
24条回答
妖精总统
2楼-- · 2019-01-01 06:43

My approach is:

.bashrc

export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME

# -- optional
# export PATH=$JAVA_HOME/bin:$PATH

This makes it very easy to switch between J6 and J7

查看更多
低头抚发
3楼-- · 2019-01-01 06:44

for mac user . java 8 should add

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

java 6 :

export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297

查看更多
路过你的时光
4楼-- · 2019-01-01 06:45

I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.

It's not automatically set up so to set it correctly on my system I run:

set -U JAVA_HOME (/usr/libexec/java_home)
查看更多
姐姐魅力值爆表
5楼-- · 2019-01-01 06:47

None of the above answers helped me. I suppose all the answers are for older OS X

For OS X Yosemite 10.10, follow these steps

Use your favorite text editor to open: ~/.bash_profile

//This command will open the file using vim
$ vim ~/.bash_profile

Add the following line in the file and save it ( : followed by "x" for vim):

export JAVA_HOME=$(/usr/libexec/java_home)

Then in the terminal type the following two commands to see output:

$ source ~/.bash_profile

$ echo $JAVA_HOME

In the second line, you are updating the contents of .bash_profile file.

查看更多
梦寄多情
6楼-- · 2019-01-01 06:48

For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.

I am sure there is a better way but got me up and running.

hughsmac:~ hbrien$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

查看更多
妖精总统
7楼-- · 2019-01-01 06:50

Update for Java 9 and some neat alias.

In .bash_profile:

export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`

Note, that for the latest version it is 9 and not 1.9.

Set active Java:

export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH

Some additional alias to switch between the different versions:

alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'

Test in terminal:

% j8
% java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
% j9
% 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)

EDIT: Update for Java 10

export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
查看更多
登录 后发表回答