云服务器:阿里云CentOS7.3 64
jdk的安装通常有两种方式,一种是通过yum安装openjdk,一种是离线安装。一般在生产环境我们安装jre环境即可。
一、通过yum安装
1、先查看下云库中的版本,目前最新版本已经到java11了(一般选择OpenJDK Runtime Environment即可)
[root@sihan ~]# yum search java|grep jdk
ldapjdk-javadoc.noarch : Javadoc for ldapjdk
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility-debug.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility-debug.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment with full debug on
java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment with full debug on
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos with full debug on
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos with full debug on
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment with full
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment with
java-1.8.0-openjdk-headless.i686 : OpenJDK Runtime Environment
2、这里我安装下java-1.8.0-openjdk.x86_64版本,安装过程需确认的输入y即可(安装完jdk的默认路径/usr/lib/jvm)
[root@sihan ~]# yum install java-1.8.0-openjdk.x86_64
[root@sihan ~]# ls /usr/lib/jvm
java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64 jre-1.8.0-openjdk jre-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64 jre-openjdk
3、设置环境变量,将下方配置保存到/etc/profile中
[root@sihan ~]# vi /etc/profile
#set java environment
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
4、让配置环境生效
[root@sihan ~]# source /etc/profile
验证
[root@sihan ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
二、离线安装
jdk下载官网:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
1、上传压缩包到服务器,并进行解压
[root@sihan ~]# tar -zxvf /home/jdk-8u202-linux-x64.tar.gz -C /usr/lib/jvm/
2、设置环境变量,将下方配置保存到/etc/profile中
[root@sihan ~]# vi /etc/profile
#set java environment
JAVA_HOME=/usr/lib/jvm/jdk1.8.0_202
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
3、让配置环境生效
[root@sihan ~]# source /etc/profile
验证
[root@sihan jvm]# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
三、卸载jdk
如果是离线安装的jdk进入相应目录将整个目录删除即可,下面展示的是移除yum安装的jdk卸载。
1、查看安装的jdk
[root@sihan ~]# yum list installed | grep java
java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 @updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-1.el7_6 @updates
javapackages-tools.noarch 3.4.1-11.el7 @base
python-javapackages.noarch 3.4.1-11.el7 @base
tzdata-java.noarch 2018i-1.el7 @updates
2、卸载
[root@sihan ~]# yum -y remove java-1.8.0-openjdk*
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 will be erased
---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-1.el7_6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================
Removing:
java-1.8.0-openjdk x86_64 1:1.8.0.191.b12-1.el7_6 @updates 501 k
java-1.8.0-openjdk-headless x86_64 1:1.8.0.191.b12-1.el7_6 @updates 104 M
Transaction Summary
========================================================================================================================================
Remove 2 Packages
Installed size: 104 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : 1:java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64 1/2
Erasing : 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-1.el7_6.x86_64 2/2
Verifying : 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-1.el7_6.x86_64 1/2
Verifying : 1:java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64 2/2
Removed:
java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-1.el7_6
Complete!
[root@sihan ~]# yum -y remove tzdata-java.noarch
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package tzdata-java.noarch 0:2018i-1.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================
Removing:
tzdata-java noarch 2018i-1.el7 @updates 365 k
Transaction Summary
========================================================================================================================================
Remove 1 Package
Installed size: 365 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : tzdata-java-2018i-1.el7.noarch 1/1
Verifying : tzdata-java-2018i-1.el7.noarch 1/1
Removed:
tzdata-java.noarch 0:2018i-1.el7
Complete!
[root@sihan ~]# java -version
-bash: /usr/bin/java: No such file or directory