tar -xzvf jdk-8u151-linux-x64.tar.gz -C /usr/local/src

sudo vim /etc/profile

1.编辑/etc/profile

# JAVA
export JAVA_HOME=/usr/local/src/jdk1.8.0_151
export JRE_HOME=$JAVA_HOME/jre
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

2.使环境变量即刻生效
$>source /etc/profile

3. 替换默认的java
$> rm -rf /etc/alternatives/java
$> ln -s /usr/local/src/jdk1.8.0_151/bin/java /etc/alternatives/java

4.进入任意目录下,测试是否ok
$>cd ~
$>java -version