1.1 系统环境:
[root@nginx-daili tools]# cat/etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@nginx-daili tools]# uname -r
3.10.0-123.el7.x86_64
[root@nginx-daili tools]# uname -n
nginx-daili
[root@nginx-daili tools]# uname -m
x86_64
1.2 下载JDK 软件并校验包
下载编译安装包
wget http://download.oracle.com/otn-pub/java/jdk/8u73-b02/jdk-8u73-linux-x64.tar.gz
校验包
[root@nginx-daili tools]# md5sumjdk-8u73-linux-x64.tar.gz
1b0120970aa8bc182606a16bf848a686 jdk-8u73-linux-x64.tar.gz
[root@nginx-daili tools]# catjdk-8u73-linux-x64.md5
jdk-8u73-linux-x64.tar.gz md5: 1b0120970aa8bc182606a16bf848a686
[root@nginx-daili tools]#
1.3 解压jdk程序
[root@nginx-daili tools]# tar zxf jdk-8u73-linux-x64.tar.gz
[root@nginx-daili tools]# mv jdk1.8.0_73 /usr/lib/
1.4 创建 jdk程序目录的软链接,方便jdk以后升级
思路,给jdk 程序目录创建软链接为jdk或jvm,此处为/usr/lib/jdk软链接 (yum 安装的jdk环境默认为/usr/lib/jvm),然后给程序中的 java 和javac创建软链接到/usr/bin/下,把默认的改名。
[root@nginx-daili tools]# ln -s /usr/lib/jdk1.8.0_73/usr/lib/jdk
[root@nginx-daili tools]# mv /usr/bin/java/usr/bin/java.bak
[root@nginx-daili tools]# ln -s /usr/lib/jdk/jre/bin/java/usr/bin/java
[root@nginx-daili tools]# ln -s /usr/lib/jdk/bin/javac/usr/bin/javac
[root@nginx-daili tools]# ll /usr/bin/java*
lrwxrwxrwx. 1 root root 27 3月 11 09:23 /usr/bin/java ->/usr/local/jdk/jre/bin/java
lrwxrwxrwx. 1 root root 22 3月 11 08:00 /usr/bin/java.bak-> /etc/alternatives/java
lrwxrwxrwx. 1 root root 24 3月 11 09:23 /usr/bin/javac-> /usr/local/jdk/bin/java
创建 java 和javac软链接,方便软件升级
[root@nginx-daili tools]# ll /usr/lib/java*
lrwxrwxrwx. 1 root root 35 3月 11 09:13 /usr/local/java ->/usr/local/jdk1.8.0_73/jre/bin/java
lrwxrwxrwx. 1 root root 32 3月 11 09:13 /usr/local/javac ->/usr/local/jdk1.8.0_73/bin/javac
1.5 设置java环境变量
cp -p /etc/profile /etc/profile.bak$(date+%F)
[root@nginx-daili usr]# tail -5 /etc/profile
##set environment
export JAVA_HOME=/usr/lib/jdk
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
1.6 设置默认JDK(可选操作)
由于系统中可能会有默认的其他版本JDK,所以,为了将我们安装的JDK设置为默认JDK版本,还要进行如下工作。
update-alternatives --install /usr/bin/javajava /usr/lib/jvm/java7/bin/java 300
update-alternatives --install/usr/bin/javac javac /usr/lib/jvm/java7/bin/javac 300
update-alternatives --install /usr/bin/jarjar /usr/lib/jvm/java7/bin/jar 300
update-alternatives --install/usr/bin/javah javah /usr/lib/jvm/java7/bin/javah 300
update-alternatives --install/usr/bin/javap javap /usr/lib/jvm/java7/bin/javap 300
执行下面命令,设置默认版本,此命令执行后,系统会列出当前存在的各种JDK版本,会提示你选择
# update-alternatives --config java
1.7 测试成功
[root@nginx-daili usr]# which java
/usr/lib/jdk/bin/java
[root@nginx-daili usr]# which javac
/usr/lib/jdk/bin/javac
[root@nginx-daili usr]# java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build25.73-b02, mixed mode)