说明
- 如果是用bash,修改~/.bash_profile或~/.profile。
两者的区别:
.bash_profile是用户级别的,更新系统或切换用户就会失效。
.profile是系统级别的,永远都有效。
2.如果是用zsh,修改~/.zshrc。
step1:检查是否安装了jdk(Mac系统默认已安装)
1.查看java的安装路径
which java
2.查看java版本
java -version
step2:设置环境变量
以bash为例:
## edit .bash_profile to set java_home variable
vim ~/.bash_profile
## add the following line into the file
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
## let the configuration in .bash_profile take effect
source .bash_profile
以zsh为例:
## check all the available jdk
/usr/libexec/java_home -V
## check the top jdk
/usr/libexec/java_home
## check some jdk (eg. version 1.7)
/usr/libexec/java_home -v 1.7
## edit .zshrc to set java_home variable
vim ~/.zshrc
## add the following line into the file
export JAVA_HOME=$(/usr/libexec/java_home)
## you can also use the following line to set the variable, which is not recommended, only for older mac os.
## export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
##let the configuration in .zshrc take effect
source ~/.zshrc
## check if configure succeed
echo $JAVA_HOME