CentOS 7上安装Java JDK

JDK官网下载页面:

本次下载安装版本为:jdk1.8.0_201

1
wget https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz?AuthParam=1553435391_8796bf6e0ec2984c2635f13bda307604 --no-check-certificate

注意:使用wget下载后的文件名可能异常,需要重命名(可同时移动到安装目录)

1
2
mv jdk-8u201-linux-x64.tar.gz\?AuthParam\=1512975825_b6f4d6db4f306de31c42215c4b2fec67
/opt/jdk-8u201-linux-x64.tar.gz

解压:

1
tar zxvf jdk-8u201-linux-x64.tar.gz

进入解压后的目录,并确保当前路径为:/opt/jdk1.8.xxxx

1
2
[root@localhost jdk1.8.0_201]# pwd
/usr/local/jdk1.8.0_201

添加环境变量

vim /etc/profile.d/java.sh

1
2
3
4
5
6
JAVA_HOME=/opt/jdk1.8.0_201/
JAVA_BIN=/opt/jdk1.8.0_201/bin
JRE_HOME=/opt/jdk1.8.0_201/jre
PATH=$PATH:/opt/jdk1.8.0_201/bin:/opt/jdk1.8.0_201/jre/bin
CLASSPATH=/opt/jdk1.8.0_201/jre/lib:/opt/jdk1.8.0_201/lib:/opt/jdk1.8.0_201/jre/lib/charsets.jar
export JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH

source环境变量文件

1
source /etc/profile.d/java.sh

查看java版本

1
2
3
4
[root@localhost jdk1.8.0_201] # java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

也可以按如下格式写环境变量配置:

1
2
3
4
JAVA_HOME=/opt/jdk1.8.0_201/
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$PATH:$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH CLASSPATH