linux下安装JDK

1)查看是否安装JDK
命令#:rpm -qa|grep java
存在,显示如下信息:
java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
2)执行下面代码删除:
#:rpm -e –nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
#:rpm -e –nodeps java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
3)查看yum库中都有哪些jdk版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# 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-demo.x86_64 : OpenJDK Demos
  java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
  java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
  java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
  java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
  ldapjdk.x86_64 : The Mozilla LDAP Java SDK

4)选择版本,进行安装
[root@localhost ~]# yum install java-1.7.0-openjdk
安装完之后,默认的安装目录是在: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64

5)设置环境变量
[root@localhost ~]# vi /etc/profile
添加如下内容:(按 i 进入编辑,编辑完成 esc 退出编辑, 回车输入 :wq 保存,ok)

1
2
3
4
5
6
#set java environment
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64
JRE_HOME=$JAVA_HOME/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@localhost java]# source /etc/profile
6)验证

1
2
3
4
  [root@localhost java]# java -version
  java version "1.7.0_79"
  Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
  Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
ˆ Back To Top