一、下载安装JDK
从Oracle官方网站下载并安装最新版本:
Centos默认是openjdk, 我们建议生产环境使用Oracle JDK 8。 最新版本是 1.8.0_152:
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie"
"http://download.oracle.com/otn-pub/java/jdk/8u152-b16/aa0333dd3019491ca4f6ddbe78cdb6d0/jdk-8u152-linux-x64.rpm"
sudo yum localinstall -y jdk-8u144-linux-x64.rpm
二、配置环境变量
查看安装的jdk的位置:
[jigsaw@workspace download]$ ls -al /usr/java
total 0
drwxr-xr-x. 3 root root 55 Dec 2 04:23 .
drwxr-xr-x. 14 root root 167 Sep 14 05:31 ..
lrwxrwxrwx. 1 root root 16 Dec 2 04:23 default -> /usr/java/latest
drwxr-xr-x. 9 root root 268 Dec 2 04:23 jdk1.8.0_151
lrwxrwxrwx. 1 root root 22 Dec 2 04:23 latest -> /usr/java/jdk1.8.0_151
将jdk添加到java的alternatives中
[jigsaw@workspace download]$ sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 2
几个参数都比较好懂,就是将可执行文件/usr/java/default/bin/java软链接到/usr/bin/java,配置名为java,最后一个参数2是因为我们之前就看到,我们已经有了一个java了,所以index选择为2。继续配置:
[jigsaw@workspace download]$ sudo alternatives --config java
There are 4 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.151-2.6.11.1.el7_4.x86_64/jre/bin/java)
*+ 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64/jre/bin/java)
3 /usr/java/jdk1.8.0_152/jre/bin/java
4 /usr/java/default/bin/java
Enter to keep the current selection[+], or type selection number: 4
输入4, 即我们刚安装的这个。 之后继续修改java, javac,jar等变量的默认位置:
```bash
[jigsaw@workspace download]$ sudo alternatives --install /usr/bin/jar jar /usr/java/default/bin/jar 2
[jigsaw@workspace download]$ sudo alternatives --install /usr/bin/javac javac /usr/java/default/bin/javac 2
[jigsaw@workspace download]$ sudo alternatives --set jar /usr/java/default/bin/jar
[jigsaw@workspace download]$ sudo alternatives --set javac /usr/java/default/bin/javac
确认下是否设置成功:
[jigsaw@workspace download]$ java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
[jigsaw@workspace download]$ javac -version
javac 1.8.0_152
设置常用的环境变量:
[jigsaw@workspace download]$ sudo vi ~/.bash_profile
内容是:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin