JDK安裝

本文章內容:

1. 安裝OpenJDK
    1.1 使用yum安裝
    1.2 從官網下載安裝

2. 安裝OracleJDK

 

1. 安裝OpenJDK

1.1 使用yum安裝

1.1.1 使用yum list all | grep jdk查看可安裝的JDK版本

[root@localhost ~]# yum list all | grep jdk
copy-jdk-configs.noarch                     3.3-10.el7_5               @base    
java-1.6.0-openjdk.x86_64                   1:1.6.0.41-1.13.13.1.el7_3 base     
java-1.6.0-openjdk-demo.x86_64              1:1.6.0.41-1.13.13.1.el7_3 base     
java-1.6.0-openjdk-devel.x86_64             1:1.6.0.41-1.13.13.1.el7_3 base     
java-1.6.0-openjdk-javadoc.x86_64           1:1.6.0.41-1.13.13.1.el7_3 base     
java-1.6.0-openjdk-src.x86_64               1:1.6.0.41-1.13.13.1.el7_3 base     
java-1.7.0-openjdk.x86_64                   1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-accessibility.x86_64     1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-demo.x86_64              1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-devel.x86_64             1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-headless.x86_64          1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-javadoc.noarch           1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.7.0-openjdk-src.x86_64               1:1.7.0.261-2.6.22.2.el7_8 updates  
java-1.8.0-openjdk.i686                     1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk.x86_64                   1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-accessibility.i686       1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-accessibility.x86_64     1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-demo.i686                1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-demo.x86_64              1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-devel.i686               1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-devel.x86_64             1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-headless.i686            1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-headless.x86_64          1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-javadoc.noarch           1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-javadoc-zip.noarch       1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-src.i686                 1:1.8.0.262.b10-0.el7_8    updates  
java-1.8.0-openjdk-src.x86_64               1:1.8.0.262.b10-0.el7_8    updates  
java-11-openjdk.i686                        1:11.0.8.10-0.el7_8        updates  
java-11-openjdk.x86_64                      1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-demo.i686                   1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-demo.x86_64                 1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-devel.i686                  1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-devel.x86_64                1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-headless.i686               1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-headless.x86_64             1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-javadoc.i686                1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-javadoc.x86_64              1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-javadoc-zip.i686            1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-javadoc-zip.x86_64          1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-jmods.i686                  1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-jmods.x86_64                1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-src.i686                    1:11.0.8.10-0.el7_8        updates  
java-11-openjdk-src.x86_64                  1:11.0.8.10-0.el7_8        updates  
ldapjdk.noarch                              4.19-5.el7                 base     
ldapjdk-javadoc.noarch                      4.19-5.el7                 base   

1.1.2 使用yum安裝

[root@localhost ~]# yum install java-11-openjdk.x86_64 java-11-openjdk-devel.x86_64 -y
[root@localhost ~]# java -version
openjdk version "11.0.8" 2020-07-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.8+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode, sharing)

1.2 從官網下載安裝

1.2.1 方法一:訪問OpenJDK官網"https://openjdk.java.net/" → 點下圖紅框鏈結

or 方法二:拉到下面點JDK 8 Updates可以下載最新更新版

1.2.2 方法一:選想下載的JDK版本

or 方法二:點Wiki

1.2.3 方法一:滑鼠右鍵檔案鏈結 → 點"複製鏈結網址"

or 方法二:找最新更新日期,點Binaries

1.2.4 方法一:跳到1.2.5。方法二:滑鼠右鍵檔案鏈結 → 點"複製鏈結網址"

1.2.5 使用wget下載到本地(下面是方法一的後續,方法二同方法一即可)

[root@localhost ~]# wget https://download.java.net/openjdk/jdk15/ri/openjdk-15+36_linux-x64_bin.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  openjdk-15+36_linux-x64_bin.tar.gz

1.2.6 解壓縮下載的JDK檔並放置到/var/lib/java目錄下

[root@localhost ~]# mkdir /var/lib/java
[root@localhost ~]# tar xf openjdk-15+36_linux-x64_bin.tar.gz -C /var/lib/java/
[root@localhost ~]# ls /var/lib/java/
jdk-15
drwxr-xr-x. 8 root root 96 Sep 12 16:56 jdk-15

1.2.7 創建鏈接以便於日後版本更換

[root@localhost ~]# ln -sv /var/lib/java/jdk-15 /var/lib/java/java
‘/var/lib/java/java’ -> ‘/var/lib/java/jdk-15’
[root@localhost ~]# ll /var/lib/java/
total 0
lrwxrwxrwx. 1 root root 20 Sep 12 17:00 java -> /var/lib/java/jdk-15
drwxr-xr-x. 8 root root 96 Sep 12 16:56 jdk-15

1.2.8 設定$PATH環境

[root@tomcat-server1 ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/var/lib/java/java
export PATH=$JAVA_HOME/bin:$PATH
[root@tomcat-server1 ~]# . /etc/profile.d/java.sh  

1.2.9 結論

方法一是從SE頁面下載JDK,所下載的不是最新版本,而方法二可以下載到最新版本。

 

2. 安裝OracleJDK

2.1 訪問Oracle官網"https://www.oracle.com" → 搜索"JDK" → 點"JDK download"鏈結

2.2 點要下載的JDK

2.3 點Linux RPM包的下載鏈結

2.4 打勾接受License聲明條款 → 點下載鏈結

2.5 輸入註冊Oracle的帳號、密碼

2.6 保存到本地

2.7 使用FileZilla軟體將下載的JDK檔傳到Linux主機上

[root@localhost ~]# ls
anaconda-ks.cfg  jdk-14.0.2_linux-x64_bin.rpm

2.8 安裝JDK

[root@localhost ~]# yum install jdk-14.0.2_linux-x64_bin.rpm -y
[root@tomcat-server1 ~]# java -version
java version "14.0.2" 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)