Mac上设置Java环境变量

说明

  1. 如果是用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

<h3>The End:</h3>

  1. 我的:简书
  2. 我的:GitHub
  3. 我的:博客
  4. 我的:CSDN
  5. Email:1619153872@qq.com
  6. 微信:

    《Mac上设置Java环境变量》 扫一扫上面的二维码,加我微信

    原文作者:妞妞骑毛驴
    原文地址: https://www.jianshu.com/p/568a7c56abab
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞