1.查看是否已安装JDK
(1)查看是否已安装JDK
# yum list installed |grep java
(2)卸载CentOS系统Java环境
# yum -y remove java-1.8.0-openjdk* //*表示卸载所有openjdk相关文件输入
# yum -y remove tzdata-java.noarch //卸载tzdata-java
(3)查看JDK软件包版本
# yum -y list java* 或者使用# yum search java | grep -i --color JDK 查看JDK软件
2.下载安装JDK
(1)安装JDK
安装java1.8.0所有程序
# yum install java-1.8.0-openjdk*
yum install -y java-1.8.0-openjdk-devel.x86_64
或者安装OpenJDK11
yum -y install java-11-openjdk.x86_64 java-11-openjdk-headless.x86_64 java-11-openjdk-devel.x86_64
注意:输入确认(y)直到控制台返回Complete安装成功
(2)查看java版本信息
# java -version
输入javac java命令后显示帮助信息就算是安装成功了
使用yum安装环境变量自动就配好了
(3)查看jdk安装位置
[root@VM-0-3-centos ~]# rpm -qa | grep java
javapackages-tools-3.4.1-11.el7.noarch
python-javapackages-3.4.1-11.el7.noarch
tzdata-java-2021e-1.el7.noarch
java-11-openjdk-11.0.14.0.9-1.el7_9.x86_64
java-11-openjdk-headless-11.0.14.0.9-1.el7_9.x86_64
3.配置更改jdk环境变量
(1)修改配置文件/etc/profile
# vim /etc/profile
export JAVA_HOME=/ymworkspace/yzmeta/deploy/java-se-8u41-ri
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
或者如果安装的是JDK11,则追加如下信息
# OpenJDK11
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-1.el7_9.x86_64
JRE_HOME=$JAVA_HOME
CLASS_PATH=.:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
(2)使配置生效
source /etc/profile
(3)查看java环境变量
[root@VM-0-3-centos ~]# echo $JAVA_HOME
/yzmeta/deploy/java-se-8u41-ri
[root@VM-0-3-centos ~]# which java
/yzmeta/deploy/java-se-8u41-ri/bin/java