CentOS下有三种方式安装JDK。
下载压缩包安装
1. 下载压缩包参考Linux使用wget和curl下载jdk。
2. 新建安装目录,并解压安装包到安装目录
[root@iZ9 ~]# mkdir/usr/java
[root@iZ9 ~]# cd /usr/java
[root@iZ9 ~]# tar -zxvf jdk-8u144-linux-x64.tar.gz
3. 设置环境变量
[root@iZ9 java]# vim /etc/profile
在profile添加以下内容:
#set java environment
JAVA_HOME=/usr/java/jdk1.8.0_144
JRE_HOME=/usr/java/jdk1.8.0_144/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
令环境变量生效
[root@iZ9 java]# source /etc/profile
[root@iZ9 java]# java -version
java version "1.8.0_144"
rpm安装
1.下载rpm包,参考Linux使用wget和curl下载jdk。
2. 安装rpm包
[root@iZ9 ~]# rpm -ivh jdk-8u144-linux-x64.rpm
rpm默认会把jdk安装在/usr/java/jdk1.8.0_144,然后通过链接到/usr/bin,所以不需要设置环境变量即可使用java,如需设置环境变量参考压缩包安装方式
yum安装
1、yum search搜索jdk包
[root@iZ9 ~]$ yum search java | grep jdk
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment with full debug on
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos with full debug on
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment with
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-headless-debug.x86_64 : OpenJDK Runtime Environment with full
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-javadoc-debug.noarch : OpenJDK API Documentation for packages
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk-src-debug.x86_64 : OpenJDK Source Bundle for packages with
ldapjdk.x86_64 : The Mozilla LDAP Java SDK
2. 选中合适的版本安装
[root@iZ9 ~]$ yum install java-1.8.0-openjdk
yum安装会把jdk安装在/usr/lib/jvm/java-1.8.0-openjdk.x86_64下,并通过链接到/usr/bin,所以不需要设置环境变量就可以直接使用。如需设置环境变量,参考压缩包安装方式。
另外,yum安装使用了alternatives来管理多个版本的软件,默认会链接到最新安装的jdk