安装oracle官方JDK

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装oracle官方JDK相关的知识,希望对你有一定的参考价值。

Oracle JDK的rpm安装

#需要登录下载:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
[root@cen7_17 ~]# ls -lh jdk-8u291-linux-x64.rpm
-rw-r--r-- 1 root root 109M Jun 23 07:06 jdk-8u291-linux-x64.rpm

#安装jdk,无相关依赖包
[root@cen7_17 ~]# yum install -y jdk-8u291-linux-x64.rpm 
[root@cen7_17 ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

#初始化环境变量
[root@cen7_17 ~]# vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
#以下两项非必须项,不过建议配上
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/

[root@cen7_17 ~]# source /etc/profile.d/jdk.sh

#查看jdk信息
[root@cen7_17 ~]# which java
/usr/java/default/bin/java
[root@cen7_17 ~]# ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Jun 24 06:51 /usr/bin/java -> /etc/alternatives/java
[root@cen7_17 ~]# ll /etc/alternatives/java
lrwxrwxrwx 1 root root 37 Jun 24 06:51 /etc/alternatives/java -> /usr/java/jdk1.8.0_291-amd64/bin/java

#查看到安装目录为/usr/java下
[root@cen7_17 ~]# rpm -ql jdk1.8 |less
/usr
/usr/java
/usr/java/jdk1.8.0_291-amd64
/usr/java/jdk1.8.0_291-amd64/.java
/usr/java/jdk1.8.0_291-amd64/.java/.systemPrefs
/usr/java/jdk1.8.0_291-amd64/.java/.systemPrefs/.system.lock
/usr/java/jdk1.8.0_291-amd64/.java/.systemPrefs/.systemRootModFile
/usr/java/jdk1.8.0_291-amd64/.java/init.d
/usr/java/jdk1.8.0_291-amd64/.java/init.d/jexec
/usr/java/jdk1.8.0_291-amd64/COPYRIGHT
/usr/java/jdk1.8.0_291-amd64/LICENSE
/usr/java/jdk1.8.0_291-amd64/README.html
/usr/java/jdk1.8.0_291-amd64/THIRDPARTYLICENSEREADME-JAVAFX.txt
/usr/java/jdk1.8.0_291-amd64/THIRDPARTYLICENSEREADME.txt
/usr/java/jdk1.8.0_291-amd64/bin
/usr/java/jdk1.8.0_291-amd64/bin/ControlPanel
/usr/java/jdk1.8.0_291-amd64/bin/appletviewer

[root@cen7_17 ~]# ll /usr/java/
total 0
lrwxrwxrwx 1 root root  16 Jun 24 06:51 default -> /usr/java/latest
drwxr-xr-x 9 root root 286 Jun 24 06:51 jdk1.8.0_291-amd64
lrwxrwxrwx 1 root root  28 Jun 24 06:51 latest -> /usr/java/jdk1.8.0_291-amd64

Oracle JDK的二进制文件安装

#下载安装包:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
[root@cen7_27 ~]# tar xvf jdk-8u291-linux-x64.tar.gz -C /usr/local/
[root@cen7_27 ~]# cd /usr/local/
[root@cen7_27 local]# ls
bin  etc  games  include  jdk1.8.0_291  lib  lib64  libexec  sbin  share  src
[root@cen7_27 local]# ln -s jdk1.8.0_291/ jdk

#初始化环境变量
[root@cen7_27 ~]# vim /etc/profile.d/jdk.sh
[root@cen7_27 ~]# cat /etc/profile.d/jdk.sh 
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
#以下两项非必须项
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
[root@cen7_27 ~]# source /etc/profile.d/jdk.sh 

#注意:JAVA_HOME变量必须设置,否则tomcat启动时会出下面错误
[root@cen7_27 ~]#catalina.sh
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
[root@centos8 ~]#startup.sh 
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

#验证安装
[root@cen7_27 ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@cen7_27 ~]# which java
/usr/local/jdk/bin/java

一键安装二进制的JDK

[root@cent8_yzl_50 ~]# cat install_jdk.sh 
#!/bin/bash
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
JDK_DIR="/usr/local"

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\\\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\\\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\\\033[1;32m"
    SETCOLOR_WARNING="echo -en \\\\033[1;33m"
    SETCOLOR_NORMAL="echo -en \\E[0m"
    echo -n "$2" && $MOVE_TO_COL
    echo -n "["
    if [ $1 = "success" -o $1 = "0" ] ;then
	    ${SETCOLOR_SUCCESS}
	    echo -n $" OK "
    elif [ $1 = "failure" -o $1 = "1" ] ; then
	    ${SETCOLOR_FAILURE}
	    echo -n $"FAILED"
    else
	    ${SETCOLOR_WARNING}
	    echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

install_jdk () {
if ! [ -f "$DIR/$JDK_FILE" ] ;then
	color 1 "$JDK_FILE 文件不存在"
	exit;
elif [ -d $JDK_DIR/jdk ] ;then
	color 1 "JDK 已经安装"
        exit;
else  
	[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk

cat > /etc/profile.d/jdk.sh << EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\\$JAVA_HOME/jre
export CLASSPATH=\\$JAVA_HOME/lib/:\\$JRE_HOME/lib/
export PATH=\\$PATH:\\$JAVA_HOME/bin
EOF
source /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || { color 1 "JDK 安装失败" ; exit;}

}

install_jdk
[root@cent8_yzl_50 ~]# 
[root@cent8_yzl_50 ~]# chmod +x install_jdk.sh 
[root@cent8_yzl_50 ~]# bash install_jdk.sh 

以上是关于安装oracle官方JDK的主要内容,如果未能解决你的问题,请参考以下文章

tomcat的安装

linux 中 jdk 的安装和卸载

deepin15.11安装Oracle JDK

JDK的安装与配置

Ubuntu安装JDK

初学java,jdk for mac如何配置 如何使用