MacBook 安装jdk9,并配置运行环境

时间:2024-10-10 07:03:36

MacBook 安装jdk9[jdk-9.0.4_osx-x64_bin.dmg],并配置java运行环境

  • 下载jdk9
  • 检验Java环境以及jdk9是否安装成功
  • Mac下查看安装的jdk目录
  • 配置环境变量
    • 法1
    • 法2
  • jdk的安装路径

下载jdk9

下载jdk9.
在这里插入图片描述
选择“Accept License Agreement ”,然后点击macOS版本下载;
然后安装即可

检验Java环境以及jdk9是否安装成功

检验java环境终端输入:java -version,显示版本号

检验jdk安装是否成功:javac -version

MacdeMacBook-Pro:~ mac$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
MacdeMacBook-Pro:~ mac$ javac -version
javac 9.0.4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Mac下查看安装的jdk目录

打开终端,输入:/usr/libexec/java_home -V

注意:输入命令参数区分大小写(-v是不对的,必须是-V)

MacdeMacBook-Pro:~ mac$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    9.0.4, x86_64:	"Java SE 9.0.4"	/Library/Java/JavaVirtualMachines/jdk9.0.4.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
  • 1
  • 2
  • 3
  • 4
  • 5

可看到jdk的安装路径为:
/Library/Java/JavaVirtualMachines/jdk-9.0./Contents/Home

配置环境变量

jdk10开始安装时会自动配置环境变量无需手动配置

法1

在终端中输入:sudo vi /etc/profile

MacdeMacBook-Pro:~ mac$ sudo vi /etc/profile
Password:


# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
~                                                                                                                                                                                  
~                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                          
"/etc/profile" [readonly] 9L, 189C

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

此时,按下键盘上字母键 i ,出现如下提示:

– INSERT – W10: Warning: Changing a readonly file

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
~                                                                                                                                                                                  
                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                                                                 
-- INSERT -- W10: Warning: Changing a readonly file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

回车

-- INSERT -- W10: Warning: Changing a readonly file
E325: ATTENTION
Found a swap file by the name "/private/etc/."
          owned by: root   dated: Fri Aug  2 14:26:11 2019
         file name: /private/etc/profile
          modified: YES
         user name: root   host name: MacdeMacBook-Pro.local
        process ID: 462
While opening file "/private/etc/profile"
             dated: Mon Oct 27 13:14:14 2014

(1) Another program may be editing the same file.
    If this is the case, be careful not to end up with two
    different instances of the same file when making changes.
    Quit, or continue with caution.

(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r /private/etc/profile"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/private/etc/."
    to avoid this message.

Press ENTER or type command to continue
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

按回车,进入编辑器中

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc

fi
~                                                                                                                                                                                                                                                                                                                                                               
-- INSERT --
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

填写环境变量的相关信息:

#此处以jdk9安装路径为例,

JAVA_HOME="/Library/Java/JavaVirtualMachines//Contents/Home"

CLASS_HOME="$JAVA_HOME/lib"

PATH=".;$PATH:$JAVA_HOME/bin"

export JAVA_HOME

export CLASSPATH

export PATH
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc

fi
JAVA_HOME="/Library/Java/JavaVirtualMachines//Contents/Home"

CLASS_HOME="$JAVA_HOME/lib"

PATH=".;$PATH:$JAVA_HOME/bin"

export JAVA_HOME

export CLASSPATH

export PATH
~                                                                                                                                                                                                                                                                                                                                                                    
~                                                                                                                                                                                  
-- INSERT --
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

填写完毕后,按“ESC”退出编辑状态:下方的–insert–消失;
直接键入“:wq!”

终端输入source /etc/profile,更新
终端输入echo $JAVA_HOME,检查是否配置成功

MacdeMacBook-Pro:~ mac$ sudo vi /etc/profile
Password:
MacdeMacBook-Pro:~ mac$ source /etc/profile
MacdeMacBook-Pro:~ mac$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
MacdeMacBook-Pro:~ mac$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

法2

直接在家目录的隐藏文件中进行配置

MacdeMacBook-Pro:/ mac$ cd
MacdeMacBook-Pro:~ mac$ ls -a
.			.bashrc			.local			Desktop			
..			.conda			.mplayer		Documents		anaconda3
.CFUserTextEncoding	.condarc		.oracle_jre_usage	Downloads		
.DS_Store		.config			.python_history		Library			
.Trash			.cups			.sogouinput		Movies			百度云同步盘
.anaconda		.gitconfig		.ssh			Music
.bash_history		.ipynb_checkpoints	.viminfo		Pictures
.bash_profile		.ipython		Applications		Public
MacdeMacBook-Pro:~ mac$ vim .bash_profile 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在隐藏文件中进行配置

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
export PATH="/Users/mac/anaconda3/bin:$JAVA_HOME/bin:$PATH"
  • 1
  • 2

配置完以后,使用esc键退出编辑状态,输入“:wq”,保存并退出。
验证运行环境配置是否成功

MacdeMacBook-Pro:~ mac$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
  • 1
  • 2
  • 3
  • 4

jdk的安装路径

在这里插入图片描述
bin目录下存放JDK用于开发的一些终端命令工具。常见的工具如:
“javac”的作用是将java源文件编译为class文件(即自解码文件);
“java”命令的作用是运行class文件。
include目录下是一些C语言的头文件;
lib目录下存放JDK开发工具所依赖的一些库文件;