JDK 安装 配置

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

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

1
2
3
4
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

1
2
3
4
5
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

(3) 安装JDK

1
2
3
4
5
6
7
8
9
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位置 (可以忽略该步骤)

1
2
3
4
whereis java
which java (java执行路径)
echo $JAVA_HOME
echo $PATH

整个 /etc/profile 如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    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文件如下

1
2
3
4
5
6
7
8
9
10
11
12
# .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