linux安装jdk1.8并配置环境变量
1. 创建安装jdk的文件目录
[root@localhost /]# mkdir -p java/jdk
[root@localhost /]# ls
bin boot dev dump.rdb etc home java lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]# cd java/jdk/
[root@localhost jdk]# pwd
/java/jdk
2. 下载jdk压缩包(本文下载的是tar.gz的文件,不需要安装)
注意:要先切换目录到jdk文件夹下,然后再运行下载命令
下载命令:wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
注意:如果wget命令不能用,报错:-bash: wget: command not found。执行一下该命令(安装依赖包) yum -y install wget
[root@localhost jdk]# pwd
/java/jdk
[root@localhost jdk]# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
--2021-12-07 10:42:30-- http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
正在解析主机 download.oracle.com (download.oracle.com)... 23.3.108.27
正在连接 download.oracle.com (download.oracle.com)|23.3.108.27|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Moved Temporarily
位置:https://edelivery.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz [跟随至新的 URL]
--2021-12-07 10:42:30-- https://edelivery.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
正在解析主机 edelivery.oracle.com (edelivery.oracle.com)... 184.26.251.178, 2600:140b:2:99b::366, 2600:140b:2:9a8::366
正在连接 edelivery.oracle.com (edelivery.oracle.com)|184.26.251.178|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Moved Temporarily
位置:https://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz?AuthParam=1638845071_5ca818a87f7622905d05c2c121410a35 [跟随至新的 URL]
--2021-12-07 10:42:31-- https://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz?AuthParam=1638845071_5ca818a87f7622905d05c2c121410a35
正在连接 download.oracle.com (download.oracle.com)|23.3.108.27|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:185540433 (177M) [application/x-gzip]
正在保存至: “jdk-8u131-linux-x64.tar.gz”
8% [============> ] 15,176,255 938KB/s 剩余 3m 9s
下载命令运行后,等待下载完成
3. 解压
[root@localhost jdk]# pwd
/java/jdk
[root@localhost jdk]# ls
jdk-8u131-linux-x64.tar.gz
[root@localhost jdk]# tar -zxvf jdk-8u131-linux-x64.tar.gz
[root@localhost jdk]# pwd
/java/jdk
[root@localhost jdk]# ls
jdk1.8.0_131 jdk-8u131-linux-x64.tar.gz
解压完成后,会生成jdk1.8.0.131文件夹,输入命令:mv jdk1.8.0.131 jdk1.8 重命名一下,不改名称也行,看个人习惯
[root@localhost jdk]# mv jdk1.8.0_131 jdk1.8
[root@localhost jdk]# ls
jdk1.8 jdk-8u131-linux-x64.tar.gz
4. 配置环境变量(其实就是修改文件内容)
[root@localhost jdk]# pwd
/java/jdk
[root@localhost jdk]# vi /etc/profile
打开文件后,拉到最下面添加以下内容
export JAVA_HOME=/java/jdk/jdk1.8
export PATH=$PATH:$JAVA_HOME/bin
5. 查看环境配置是否成功
[root@localhost jdk]# pwd
/java/jdk
[root@localhost jdk]# java -version
-bash: java: 未找到命令
6. 解决遇到的问题
1、考虑是不是在根目录新建的java目录不行导致的,所以将java目录移动到/usr/local/下
[root@localhost jdk]# cd /
[root@localhost /]# pwd
/
[root@localhost /]# mv java /usr/local/
[root@localhost /]# ls
bin boot dev dump.rdb etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]# cd /usr/local/
[root@localhost local]# ls
bin etc games include java lib lib64 libexec redis-4.0.6 sbin share src
[root@localhost local]#
1.1、修改环境变量
[root@localhost local]# vi /etc/profile
将第4步配置的内容改为如下配置,并保存
# 2021-12-07 10:54 add
# JDK
JAVA_HOME=/usr/local/java/jdk/jdk1.8
JRE_HOME=/usr/local/java/jdk/jdk1.8/jre
#JAVA_HOME=/usr/local/java/jdk/jdk1.8/
#JRE_HOME=/usr/local/java/jdk/jdk1.8/jre
#PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
1.2、验证java环境变量
[root@localhost ~]# java
-bash: java: 未找到命令
还是没配置好,重启一下服务器试试
重启之后好使了,那就配置好环境变量记得重启一下吧
[root@localhost ~]# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)