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

本文指导如何在Mac上安装jdk9,并详细介绍了安装过程、验证安装及配置环境变量的方法,包括两种配置环境变量的途径。通过java -version和javac -version检查版本,通过特定命令查看jdk安装路径,并解释了bin、include和lib目录的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载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

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

可看到jdk的安装路径为:
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/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

此时,按下键盘上字母键 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

回车

-- INSERT -- W10: Warning: Changing a readonly file
E325: ATTENTION
Found a swap file by the name "/private/etc/.profile.swp"
          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/.profile.swp"
    to avoid this message.

Press ENTER or type command to continue

按回车,进入编辑器中

# 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 --

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

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

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home"

CLASS_HOME="$JAVA_HOME/lib"

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

export JAVA_HOME

export CLASSPATH

export PATH
# 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/jdk-9.jdk/Contents/Home"

CLASS_HOME="$JAVA_HOME/lib"

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

export JAVA_HOME

export CLASSPATH

export PATH
~                                                                                                                                                                                                                                                                                                                                                                    
~                                                                                                                                                                                  
-- INSERT --

填写完毕后,按“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$ 

法2

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

MacdeMacBook-Pro:/ mac$ cd
MacdeMacBook-Pro:~ mac$ ls -a
.			.bashrc			.local			Desktop			Untitled.ipynb
..			.conda			.mplayer		Documents		anaconda3
.CFUserTextEncoding	.condarc		.oracle_jre_usage	Downloads		ipmsg.db
.DS_Store		.config			.python_history		Library			untitled.txt
.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 

在隐藏文件中进行配置

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

配置完以后,使用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)

jdk的安装路径

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值