How to install Oracle JDK 8 on CentOS 修改教程

java8-centos-example java8-centos-example

In this tutorial, we will show you how to install Oracle JDK 8 On CentOS.

Environment :

  1. CentOS 6.8
  2. Oracle JDK 8u102

Note
This guide should work on Fedora and RedHat.

1. Get Oracle JDK 8

1.1 Visit Oracle JDK download page, look for RPM version.

1.2 Copy the download link of jdk-8u102-linux-x64.rpm and wget it.

$ pwd
/home/mkyong

$ wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.rpm

2. Install Oracle JDK 8

2.1 Install with yum localinstall.

$ sudo yum localinstall jdk-8u102-linux-x64.rpm

//...
//...
//...
Installed:
  jdk1.8.0_102.x86_64 2000:1.8.0_102-fcs                                                                                                                                          

Complete!

2.2 Now the JDK should be installed at /usr/java/jdk1.8.0_102

$ cd /usr/java
$ ls -lsah
total 12K
4.0K drwxr-xr-x   3 root root 4.0K Jul 21 09:58 ./
4.0K drwxr-xr-x. 15 root root 4.0K Jun 22 22:00 ../
   0 lrwxrwxrwx   1 root root   16 Jul 21 09:58 default -> /usr/java/latest/
4.0K drwxr-xr-x   9 root root 4.0K Jul 21 09:58 jdk1.8.0_102/
   0 lrwxrwxrwx   1 root root   22 Jul 21 09:58 latest -> /usr/java/jdk1.8.0_102/

2.3 Verification

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

2.4 Delete the RPM file

$ rm ~/jdk-8u102-linux-x64.rpm

Done. Oracle JDK 8 is installed on CentOS successfully.

3. JAVA_HOME Environment Variables

This is good practice to set the JAVA_HOME environment variable.

3.1 Edit the .bash_profile, and append the export JAVA_HOME at the end of the file, for example :

.bash_profile

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

# User specific environment and startup programs

export JAVA_HOME=/usr/java/jdk1.8.0_102/
export JRE_HOME=/usr/java/jdk1.8.0_102/jre

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

export PATH

3.2 Test the $JAVA_HOME and $PATH

$ source .bash_profile 

$ echo $JRE_HOME
/usr/java/jdk1.8.0_102/jre

$ echo $JAVA_HOME
/usr/java/jdk1.8.0_102/

$ echo $PATH
/...:/usr/local/bin:/usr/X11R6/bin:/home/mkyong/bin:/usr/java/jdk1.8.0_102//bin

4. Multiple JDK installed

If the CentOS has multiple JDK installed, you can use the alternatives command to set the default java

$ sudo alternatives --config java
[sudo] password for mkyong: 

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
*+ 2           /usr/java/jdk1.8.0_102/jre/bin/java

Enter to keep the current selection[+], or type selection number:

References

  1. How to Install Oracle JDK 8 On Debian
  2. How To Install Java on CentOS and Fedora
  3. How to set JAVA environment variables in Linux or CentOS
  4. How to use the ‘alternatives’ command to Install Java 8 on CentOS/RHEL and Fedora
  5. The Difference Between Fedora, Redhat, and CentOS

如果觉得JSRUN的教程对您有用,请随意打赏。您的支持将鼓励JSRUN继续提供更好的服务

捐助JSRUN
JSRUN前端教程共编系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的本前端教程,让知识的积累变得统一完整、自成体系。 希望广大工程师能一起参与进共编,让零散在世界各处的知识点找到组织、找到家。
X
支付宝
9.99
请使用支付宝扫码支付, 有效时间 5分0秒
超过有效时间后请立即停止支付。
支付故障请联系QQ:565830900
因10.00元通道已占用,系统建议您支付9.99元
正在生成二维码, 此过程可能需要15秒钟
谢谢支持,我一定会更加努力的