1. 程式人生 > >linux通過yum安裝jdk

linux通過yum安裝jdk

解除安裝centos自帶的jdk

1.檢視當前的jdk版本,並解除安裝

複製程式碼

[email protected] opt]#  rpm -qa|grep java 
java-1.6.0-openjdk-1.6.0.37-1.13.9.4.el5_11
tzdata-java-2015g-1.el5
//解除安裝
[[email protected] opt]# rpm -e --allmatches --nodeps java-1.6.0-openjdk-1.6.0.37-1.13.9.4.el5_11
[[email protected] opt]# rpm -e --allmatches --nodeps tzdata-java-2015g-1.el5

複製程式碼

安裝Jdk

1.查詢java相關得列表

複製程式碼

[[email protected] jre-1.6.0-openjdk.x86_64]# yum -y list java*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.btte.net
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
Installed Packages
java-1.6.0-openjdk.x86_64                                        1:1.6.0.37-1.13.9.4.el5_11                              installed
Available Packages
java-1.4.2-gcj-compat.x86_64                                     1.4.2.0-40jpp.115                                       base     
java-1.4.2-gcj-compat-devel.i386                                 1.4.2.0-40jpp.115                                       base     
java-1.4.2-gcj-compat-devel.x86_64                               1.4.2.0-40jpp.115                                       base     
java-1.4.2-gcj-compat-javadoc.x86_64                             1.4.2.0-40jpp.115                                       base     
java-1.4.2-gcj-compat-src.x86_64                                 1.4.2.0-40jpp.115                                       base     
java-1.6.0-openjdk-demo.x86_64                                   1:1.6.0.37-1.13.9.4.el5_11                              updates  
java-1.6.0-openjdk-devel.x86_64                                  1:1.6.0.37-1.13.9.4.el5_11                              updates  
java-1.6.0-openjdk-javadoc.x86_64                                1:1.6.0.37-1.13.9.4.el5_11                              updates  
java-1.6.0-openjdk-src.x86_64                                    1:1.6.0.37-1.13.9.4.el5_11                              updates  
java-1.7.0-openjdk.x86_64                                        1:1.7.0.91-2.6.2.1.el5_11                               updates  
java-1.7.0-openjdk-demo.x86_64                                   1:1.7.0.91-2.6.2.1.el5_11                               updates  
java-1.7.0-openjdk-devel.x86_64                                  1:1.7.0.91-2.6.2.1.el5_11                               updates  
java-1.7.0-openjdk-javadoc.x86_64                                1:1.7.0.91-2.6.2.1.el5_11                               updates  
java-1.7.0-openjdk-src.x86_64                                    1:1.7.0.91-2.6.2.1.el5_11                               updates  
java_cup.x86_64                                                  1:0.10-0.k.6jpp.1                                       base     
java_cup-javadoc.x86_64                                          1:0.10-0.k.6jpp.1                                       base     
java_cup-manual.x86_64                                           1:0.10-0.k.6jpp.1                                       base     
javacc.x86_64                                                    4.0-3jpp.3                                              base     
javacc-demo.x86_64                                               4.0-3jpp.3                                              base     
javacc-manual.x86_64                                             4.0-3jpp.3                                              base

複製程式碼

 或    

複製程式碼

[[email protected] jre-1.6.0-openjdk.x86_64]# yum search jdk
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.btte.net
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
========================================================== Matched: jdk ==========================================================
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.x86_64 : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
gnu-crypto-sasl-jdk1.4.x86_64 : Gnu Crypto SASL API
jakarta-commons-beanutils.x86_64 : Jakarta Commons BeanUtils Package
jakarta-commons-collections.x86_64 : Jakarta Commons Collections Package
jakarta-commons-launcher.x86_64 : The Launcher Component is designed to be a cross platform Java application launcher.
ldapjdk.x86_64 : The Mozilla LDAP Java SDK
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
mockobjects-alt-jdk1.4.x86_64 : Mockable API for JDK 1.4
mockobjects-jdk1.4.x86_64 : MockObjects for 1.4 JDK

複製程式碼

2.安裝jdk

[[email protected] ~]# yum install java-1.6.0-openjdk.x86_64
 …… 
Total download size: 44 M
Is this ok [y/N]: y

3.安裝完成之後,驗證

[[email protected] ~]# java -version
java version "1.6.0_37"
OpenJDK Runtime Environment (IcedTea6 1.13.9) (rhel-1.13.9.4.el5_11-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

4.通過yum預設安裝的路徑為

  /usr/lib/jvm

複製程式碼

[[email protected] ~]# cd /usr/lib/jvm
[[email protected] jvm]# ll
total 24
drwxr-xr-x 3 root root 4096 Jan 12 20:01 java-1.6.0-openjdk-1.6.0.37.x86_64
lrwxrwxrwx 1 root root   21 Jan 12 20:01 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root   27 Jan 12 20:01 jre-1.6.0 -> /etc/alternatives/jre_1.6.0
lrwxrwxrwx 1 root root   38 Jan 12 20:01 jre-1.6.0-openjdk.x86_64 -> java-1.6.0-openjdk-1.6.0.37.x86_64/jre
lrwxrwxrwx 1 root root   29 Jan 12 20:01 jre-openjdk -> /etc/alternatives/jre_openjdk

複製程式碼

如果機器上同時安裝了多個jdk的話,java命令只能指向一個版本的jdk,為了在全域性中方便修改jdk版本

   /etc/alternatives 的目錄下面會有個java連結,指向預設需要執行的版本的jdk的bin/java 命令

[[email protected] jvm]# cd /etc/alternatives
[[email protected] alternatives]# ll
total 204
lrwxrwxrwx 1 root root 46 Jan 12 20:01 java -> /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

/etc/alternatives/java又會被軟連線到/usr/bin/java

5.將jdk的安裝路徑加入到JAVA_HOME

   方法一:

[[email protected] bin]# vi /etc/profile

在檔案最後加入:

#set java environment
JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH

修改/etc/profile之後讓其生效

[[email protected] alternatives]# . /etc/profile

注意 之後應有一個空格

方法二:

   或者是安裝上面建立java命令的形式,以方便修改

[[email protected] jvm]# cd /etc/alternatives
[[email protected] alternatives]# ln -s /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/ java_home

 建立一個/etc/alternatives/java_home的軟連線,將該連線指向到當前Jdk的根目錄

    然後將改連線的路徑加進去

#set java environment
JAVA_HOME=/etc/alternatives/java_home
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH
[[email protected] alternatives]# export |grep JAVA_HOME
declare -x JAVA_HOME="/etc/alternatives/java_home"

專注技術細節,提升自我能力。