查看JAVA_HOME
在終端中輸入
java -version
查看當前的java版本信息
使用下面的命令查看JAVA_HOME
echo $JAVA_HOME
如果沒有輸出JAVA_HOME的路徑麸恍,說明還沒有設置
可以繼續(xù)下面的設置操作
JAVA_HOME設置
首先查找java的安裝路徑
我們首先想到的就是使用
which java
或者
whereis java
我們得到的結果是
/usr/bin/java
我們到finder中打開這個目錄看到的并不是真正的java的安裝目錄
通過查找,在開發(fā)者網(wǎng)站找到了一篇文章
https://developer.apple.com/library/content/qa/qa1170/_index.html
Java Home
Many Java applications need to know the location of a $JAVA_HOME directory. The $JAVA_HOME on Mac OS X should be found using the /usr/libexec/java_home command line tool on Mac OS X 10.5 or later. On older Mac OS X versions where the tool does not exist, use the fixed path "/Library/Java/Home". The /usr/libexec/java_home tool dynamically finds the top Java version specified in Java Preferences for the current user. This path allows access to the bin subdirectory where command line tools such as java, javac, etc. exist as on other platforms. The tool /usr/libexec/java_home allows you to specify a particular CPU architecture and Java platform version when locating a $JAVA_HOME.
Another advantage of dynamically finding this path, as opposed to hardcoding the fixed endpoint, is that it is updated when a new version of Java is downloaded via Software Update or installed with a newer version of Mac OS X. For this reason, it is important that developers do not install files in the JDKs inside of /System, since the changes will be lost with subsequent updates by newer versions of Java.
To obtain the path to the currently executing $JAVA_HOME, use the java.home System property.
我們執(zhí)行以下命令查看
/usr/libexec/java_home -V
執(zhí)行結果
Matching Java Virtual Machines (1):
1.8.0_111, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
結果中的
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
就是JAVA_HOME的路徑
我們將JAVA_HOME的路徑添加到.bash_profile中
cd #切換到當前目錄下
ls -a #查看當前路徑下的所有文件,主要就是看看我們要編輯的.bash_profile是不是存在
將下面的內容添加到.bash_profile的最后
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
export CLASS_PATH=$JAVA_HOME/lib
保存完成之后使用
source .bash_profile
更新修改的內容
現(xiàn)在我們再使用上面的查看JAVA_HOME的命令查看的話,就會看到以下結果的輸出
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home