下载JDK
下载页面:戳这里 Java SE Download
如果需要历史版本,拖到页面最下方 Java Archive
可以看到
官网上提供的两种格式
安装 rpm格式的 JDK
rpm 类似windows 操作系统上的 exe执行文件, 默认安装到 /usr/java目录下
Step1 rpm -ivh 安装
[root@artisan soft_artisan]# rpm -ivh jdk-8u221-linux-x64.rpm warning: jdk-8u221-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Preparing... ################################# [100%] Updating / installing... 1:jdk1.8-2000:1.8.0_221-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar... [root@artisan soft_artisan]#
Step 2 配置 & 刷新 /etc/profile
该配置文件为 全局变量,对所有用户生效 。 也可不配置,在应用中指定JAVA_HOME
[root@artisan java]# vim /etc/profile [root@artisan java]# /etc/profile 最后 增加 #JDK export JAVA_HOME=/usr/java/jdk1.8.0_221-amd64 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar [root@artisan java]# 刷新 /etc/profile 使其生效 [root@artisan java]# source /etc/profile [root@artisan java]# [root@artisan java]# [root@artisan etc]# java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) [root@artisan etc]# [root@artisan java]# echo $JAVA_HOME /usr/java/jdk1.8.0_221-amd64 [root@artisan java]# whereis java java: /usr/bin/java /usr/java/jdk1.8.0_221-amd64/bin/java /usr/share/man/man1/java.1 [root@artisan java]# which java /usr/bin/java [root@artisan java]#
安装 tar.gz格式的JDK
#解压到/usr/local/jdk目录下 [root@artisan soft_artisan]# tar -xvzf jdk-8u221-linux-x64.tar.gz -C /usr/local/jdk [root@artisan ~]# 末尾增加JDK信息 [root@artisan ~]# vim /etc/profile #JDK export JAVA_HOME=/usr/local/jdk/jdk1.8.0_221 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar [root@artisan ~]# 刷新文件 [root@artisan ~]# source /etc/profile [root@artisan ~]# [root@artisan ~]# java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) [root@artisan ~]# [root@artisan ~]# whereis java java: /usr/local/jdk/jdk1.8.0_221/bin/java [root@artisan ~]# which java /usr/local/jdk/jdk1.8.0_221/bin/java [root@artisan ~]#
多个JDK共存时,设置默认的Java 版本
yum 或者 rpm安装的 JDK,可以使用 alternatives --config java
[root@artisan ~]# alternatives --config java There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 /usr/java/jdk1.8.0_221-amd64/jre/bin/java Enter to keep the current selection[+], or type selection number:
切换默认的JDK信息 ,不过这个时候 /etc/profile 要配置
export JAVA_HOME=/usr/java/default export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
看下rpm安装后的jdk目录结构
第二个 jdk1.8.0_221-amd64 是刚才安装Jdk的真实目录,而 latest 则是系统动态切换后指向的最新目录,它链接到了JDK的真实目录。而default文件又链接到 latest,所以 default 就是当前系统默认的JDK目录。 故 变更如上 export JAVA_HOME=/usr/java/default ,记得 source /etc/profile
演示:
[root@artisan soft_artisan]# ll total 729840 drwxr-xr-x. 7 10 143 4096 Jul 4 19:37 jdk1.8.0_221 drwxr-xr-x. 7 10 143 4096 Oct 5 18:13 jdk1.8.0_231 -rw-r--r--. 1 root root 179505388 Nov 3 02:17 jdk-8u221-linux-x64.rpm -rw-r--r--. 1 root root 195094741 Nov 3 10:27 jdk-8u221-linux-x64.tar.gz -rw-r--r--. 1 root root 178589686 Nov 3 02:02 jdk-8u231-linux-x64.rpm -rw-r--r--. 1 root root 194151339 Nov 3 10:36 jdk-8u231-linux-x64.tar.gz [root@artisan soft_artisan]# 安装 jdk1.8.221 [root@artisan soft_artisan]# rpm -ivh jdk-8u221-linux-x64.rpm warning: jdk-8u221-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Preparing... ################################# [100%] Updating / installing... 1:jdk1.8-2000:1.8.0_221-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar... [root@artisan soft_artisan]# 查看版本信息 [root@artisan soft_artisan]# java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) [root@artisan soft_artisan]# [root@artisan soft_artisan]# 安装 jdk1.8.231 [root@artisan soft_artisan]# rpm -ivh jdk-8u231-linux-x64.rpm warning: jdk-8u231-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Preparing... ################################# [100%] Updating / installing... 1:jdk1.8-2000:1.8.0_231-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar... [root@artisan soft_artisan]# 查看版本信息 [root@artisan soft_artisan]# java -version java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode) [root@artisan soft_artisan]# [root@artisan soft_artisan]# [root@artisan soft_artisan]# 切换 到 221 (输入1) [root@artisan soft_artisan]# alternatives --config java There are 2 programs which provide 'java'. Selection Command ----------------------------------------------- 1 /usr/java/jdk1.8.0_221-amd64/jre/bin/java *+ 2 /usr/java/jdk1.8.0_231-amd64/jre/bin/java Enter to keep the current selection[+], or type selection number: 1 [root@artisan soft_artisan]# 重新查看java -version 已经到了 221 [root@artisan soft_artisan]# java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) [root@artisan soft_artisan]# [root@artisan soft_artisan]#
看下rpm安装的jdk目录 , dfault 、 lates、真实安装目录
[root@artisan ~]# cd /usr/java/ [root@artisan java]# ll total 8 lrwxrwxrwx. 1 root root 16 Nov 4 03:54 default -> /usr/java/latest drwxr-xr-x. 8 root root 4096 Nov 4 03:54 jdk1.8.0_221-amd64 drwxr-xr-x. 8 root root 4096 Nov 4 03:55 jdk1.8.0_231-amd64 lrwxrwxrwx. 1 root root 28 Nov 4 03:55 latest -> /usr/java/jdk1.8.0_231-amd64 [root@artisan java]#
看下rpm安装的jdk目录 , dfault 、 lates、真实安装目录
[root@artisan ~]# cd /usr/java/ [root@artisan java]# ll total 8 lrwxrwxrwx. 1 root root 16 Nov 4 03:54 default -> /usr/java/latest drwxr-xr-x. 8 root root 4096 Nov 4 03:54 jdk1.8.0_221-amd64 drwxr-xr-x. 8 root root 4096 Nov 4 03:55 jdk1.8.0_231-amd64 lrwxrwxrwx. 1 root root 28 Nov 4 03:55 latest -> /usr/java/jdk1.8.0_231-amd64 [root@artisan java]#
经过验证,似乎不用配置 /etc/profile ,但是仍然对所有用户生效,请看如下验证
[root@artisan java]# useradd test [root@artisan java]# passwd test Changing password for user test. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@artisan java]# [root@artisan java]# su - test [test@artisan ~]$ java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) [test@artisan ~]$