JDK 安装 配置

JDK安装步骤
1.检查是否有JDK,如果有,可以用,直接完成,如果版本低或者没有,如果有open JDK(系统自带的),可以把open JDK卸载
2.如果有JSE JDK 低版本,可以忽略低版本,也可以删除低版本,安装新版本的JDK,配置/etc/profile文件就行
3.安装JDK,并配置/etc/profile文件就行 Windows系统可以通过环境变量

(1) 卸载JDK(可以省略该步骤)

rpm -qa | grep jdk   查看已安装的JDK
rpm -qa | grep gcj
yum -y remove java-1.7.0-openjdk-headless.x86_64  
rpm -e –nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115 

(2) 下载JDK

JDK 8u151
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz

JDK 8u172
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/jdk-8u172-linux-x64.tar.gz

国内华为镜像 https://repo.huaweicloud.com/java/jdk/

(3) 安装JDK

tar -zxvf jdk-8u111-linux-x64.tar.gz  #解压

vi /etc/profile #修改配置文件
在unset i  unset -f pathmunge的下面加上
export JAVA_HOME=/usr/java/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

source /etc/profile  #使配置立即生效

(4) 查看JDK位置 (可以忽略该步骤)

whereis java
which java (java执行路径)
echo $JAVA_HOME
echo $PATH

整个 /etc/profile 如下

    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

export JAVA_HOME=/usr/java/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

(5) 为单个用户配置JDK

vi .bash_profile #修改或新建.bash_profile

.bash_profile文件如下

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export JAVA_HOME=/home/admin/software/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

(6) 可能遇到的错误

bash: /usr/java/jdk1.8.0_111/bin/java: cannot execute binary file
因为JDK是64位的,操作系统是32位的,所以会报这个错

(7) Windows环境变量配置

JAVA_HOME

C:\ProfessionSofware\Java\jdk1.8.0_111

CLASSPATH

.:%JAVA_HOME%\lib

PATH

%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

References

[1] linux wget 下载 JDK