Linux JAVA JDK JRE 环境变量安装与配置

一般安装JDK JAVA环境有2种方式,下面我逐个说明。

生产环境:

$ hostnamectl
 Static hostname: localhost.localdomain
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 769ef9902d8743958a40cb81db696433
 Boot ID: ce70591a91b64e73926682db72441f7b
 Virtualization: microsoft
 Operating System: CentOS Linux 7 (Core)
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-514.2.2.el7.x86_64
 Architecture: x86-64

一、rpm Tool安装方式(相对简单)

1.下载对应的JAVA JDK RPM包 64位:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

2.开始安装:

# cd /tmp
# rpm -ivh jdk-8u144-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8.0_144-2000:1.8.0_144-fcs  ################################# [100%]
Unpacking JAR files...
        tools.jar...
        plugin.jar...
        javaws.jar...
        deploy.jar...
        rt.jar...
        jsse.jar...
        charsets.jar...
        localedata.jar...

3.RPM包安装完成,验证是否安装成功,如果有输出JDK相关版本说明已经安装成功。

# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

4.添加JAVA JDK环境变量:

# vim /etc/profile.d/java.sh

JAVA_HOME=/usr/java/jdk1.8.0_144
JRE_HOME=/usr/java/jdk1.8.0_144/jre
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$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 PATH CLASSPATH

5.永久使环境变量生效:

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

RPM安装方式已经完成。

二、JAVA JDK源码Tarball包安装方式(推荐)

1.先卸载之前通过rpm或yum安装的包,如果输出无,说明没安装,反之已安装。

# rpm -qa | grep -E '^open[jre|jdk]|j[re|dk]'
libbasicobjects-0.1.1-27.el7.x86_64
openjpeg-libs-1.5.1-16.el7_3.x86_64
gobject-introspection-1.42.0-1.el7.x86_64
openjpeg-1.5.1-16.el7_3.x86_64
jdk1.8.0_131-1.8.0_131-fcs.x86_64
pygobject3-base-3.14.0-3.el7.x86_64
openjpeg-devel-1.5.1-16.el7_3.x86_64

2.卸载已安装的JDK:

# yum -y remove jdk1.8.0_131-1.8.0_131-fcs.x86_64

3.下载对应的JDK版本Tarball包 64位并解压:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

# cd /mnt
# tar zxvf jdk-8u144-linux-x64.tar.gz
# mv jdk-8u144-linux-x64 /usr/local/
# cd /usr/local/
# ln -s jdk-8u144-linux-x64 jdk

4.添加JAVA JDK环境变量:

# vim /etc/profile.d/jdk.sh

JAVA_HOME=/usr/local/jdk
JRE_HOME=/usr/local/jdk/jre
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$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 PATH CLASSPATH

5.永久使JAVA JDK环境变量生效:

# source /etc/profile.d/jdk.sh

6.查看JAVA JDK版本输出:

# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

7.至此java jdk环境安装配置已经完成。

JAVA JDK JRE环境安装有多种方式,每一种方式都可以使用,找到适合自己的,如果要我推荐,我建议选择Tarball构建。