使用 yum -y list jav
或者 yum search jdk
查找 java
相关安装包的列表
# yum -y list 搜索演示 $ yum -y list java Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * updates: mirrors.aliyun.com Available Packages java-1.6.0-openjdk.x86_64 1:1.6.0.41-1.13.13.1.el7 ... java-1.8.0-openjdk-headless.x86_64 l:1.8.0.212.b04-0.el7_6 java-1.8.0-openjdk.x86_64 1:1.8.0.212.b04-0.el7_6 # yum search jdk 搜索演示 $ yum search jdk ====================================== N/S matched: jdk ====================================== copy-jdk-configs.noarch : JDKs configuration files copier java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8 ... java-11-openjdk-debug.x86_64 : OpenJDK Runtime Environment 11 with full debug on
从上述列表中选好一个 jdk
版本, 进行实际安装
$ yum install java-1.8.0-openjdk.x86_64 Installed: java-1.8.0-openjdk.x86_64 1:1.8.0.292.b10-1.el7_9 Dependency Installed: atk.x86_64 0:2.28.1-2.el7 copy-jdk-configs.noarch 0:3.3-10.el7_5 giflib.x86_64 0:4.1.6-9.el7 gtk-update-icon-cache.x86_64 0:3.22.30-6.el7... xorg-x11-fonts-Type1.noarch 0:7.5-9.el7 Dependency Updated: chkconfig.x86_64 0:1.7.6-1.el7 Complete!
如上即代表安装 jdk 成功
通过 yum 方式安装jdk
的软件默认路径为:/usr/lib/jvm
$ ls -l /usr/lib/jvm total 0 drwxr-xr-x 3 root root 16 Aug 3 22:53 java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64 lrwxrwxrwx 1 root root 21 Aug 3 22:53 jre -> /etc/alternatives/jre lrwxrwxrwx 1 root root 27 Aug 3 22:53 jre-1.8.0 -> /etc/alternatives/jre_1.8.0 lrwxrwxrwx 1 root root 35 Aug 3 22:53 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk lrwxrwxrwx 1 root root 51 Aug 3 22:53 jre-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64 -> java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre lrwxrwxrwx 1 root root 29 Aug 3 22:53 jre-openjdk -> /etc/alternatives/jre_openjdk
你需要将 jdk
的安装路径加入到 JAVA_HOME
添加到环境变量中
编辑文件 vi /etc/profile
在文件最后加入:
#set java environment JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64 PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME CLASSPATH PATH
修改文件之后之后让其生效执行命令 . /etc/profile
或 source /etc/profile
使得刚才添加的内容生效
配置完成过后, 需要验证上述安装配置是否成功. 执行命令 ava -version
显示 java 版本信息,则说明 JDK 安装成功
$ java -version openjdk version "1.8.0_292" OpenJDK Runtime Environment (build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
出现上述问题, 一般是搜索的时候访问 centos 国外源超时所导致. 更换成国内 yum 源就好
更换前备份下原来的 yum
源,
cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo_bak
替换 阿里云 yum
源
# 阿里云yum源: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum clean all yum makecache