在macOS 上添加 JAVA_HOME 环境变量
If you are planing to develop Java Apps on your Mac, you may have to set $JAVA_HOME environment variable.
Install the latest Java Virtual Machine
Go to How to install JDK on Mac OS X to install a new Java Virtual Machine. Download the latest Java JDK package from Oracle.
Check Java Virtual Machines
List directories in the following root -> /Library/Java/JavaVirtualMachines
$ ls -l /Library/Java/JavaVirtualMachines
drwxr-xr-x 3 root wheel 102 23 dic 20:36 jdk1.8.0_66.jdk
drwxr-xr-x 3 root wheel 102 21 mar 14:20 jdk1.8.0_74.jdk
And the version for java will be the latest.
$ java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
Set JAVA_HOME environment variable
Open your terminal, and create .bash_profile file if it does not exist.
vim .bash_profile
And write the following:
export JAVA_HOME=$(/usr/libexec/java_home)
Refresh the environment variables by running the following command, and check the $JAVA_HOME value:
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home
简单的说就是先安装JDK,然后找到 .bash_Profile (因为我不会用vim)。
在最后一行添加
export JAVA_HOME=$(/usr/libexec/java_home)
然后在命令行里输入
source .bash_profile
然后查看结果:
echo $JAVA_HOME