CentOS 使用yum工具安装JAVA SDK

如何在centos系统里安装JAVA JDK 运行时环境呢?本文将会介绍通过使用yum命令安装JAVA SDK 开发环境。
CentOS 6.x 和5.x 版本默认都安装了openJDK运行时环境。这是一个开源的java版本。
java
CentOS 默认安装的JAVA 软件包包括:
[cc lang=”php”]
java-1.7.0-openjdk – OpenJDK 运行时环境
java-1.7.0-openjdk-devel – OpenJDK 开发环境
[/cc]
[cc lang=”php”]
使用下面的命令查看yum软件库里可用的JDK 软件包:
yum search java | grep -i –color JDK
[/cc]
命令输出:
[cc lang=”php”]
[root@devops Desktop]# yum search java | grep -i –color JDK
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
icedtea-web.x86_64 : Additional Java components for OpenJDK – Java browser
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
[/cc]
CentOS 安装 Java SDK 开发包
在命令行下通过yum命令安装JAVA SDK:
[cc lang=”php”]
yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
[/cc]
命令输出:
[cc lang=”php”]
[root@devops Desktop]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package java-1.7.0-openjdk.x86_64 1:1.7.0.71-2.5.3.1.el6 will be installed
–> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64
—> Package java-1.7.0-openjdk-devel.x86_64 1:1.7.0.71-2.5.3.1.el6 will be installed
–> Running transaction check
—> Package libjpeg.x86_64 0:6b-46.el6 will be obsoleted
—> Package libjpeg-turbo.x86_64 0:1.2.1-3.el6_5 will be obsoleting
–> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository
Size
================================================================================
Installing:
java-1.7.0-openjdk x86_64 1:1.7.0.71-2.5.3.1.el6 updates 26 M
java-1.7.0-openjdk-devel x86_64 1:1.7.0.71-2.5.3.1.el6 updates 9.4 M
libjpeg-turbo x86_64 1.2.1-3.el6_5 base 174 k
replacing libjpeg.x86_64 6b-46.el6
Transaction Summary
================================================================================
Install 3 Package(s)
Total download size: 35 M
Is this ok [y/N]:
Is this ok [y/N]: y
Downloading Packages:
(1/3): java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64.rp | 26 MB 00:16
(2/3): java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86 | 9.4 MB 00:06
(3/3): libjpeg-turbo-1.2.1-3.el6_5.x86_64.rpm | 174 kB 00:00
——————————————————————————–
Total 1.6 MB/s | 35 MB 00:22
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key)
Package: centos-release-6-3.el6.centos.9.x86_64 (@anaconda-CentOS-201207061011.x86_64/6.3)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libjpeg-turbo-1.2.1-3.el6_5.x86_64 1/4
Installing : 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64 2/4
Installing : 1:java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86_64 3/4
Erasing : libjpeg-6b-46.el6.x86_64 4/4
Verifying : 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64 1/4
Verifying : libjpeg-turbo-1.2.1-3.el6_5.x86_64 2/4
Verifying : 1:java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86_64 3/4
Verifying : libjpeg-6b-46.el6.x86_64 4/4
Installed:
java-1.7.0-openjdk.x86_64 1:1.7.0.71-2.5.3.1.el6
java-1.7.0-openjdk-devel.x86_64 1:1.7.0.71-2.5.3.1.el6
libjpeg-turbo.x86_64 0:1.2.1-3.el6_5
Replaced:
libjpeg.x86_64 0:6b-46.el6
Complete!
[/cc]
CentOS设置 JAVA_HOME 环境变量
java SDK包默认会安装在“/usr/lib/jvm” 目录下:
[cc lang=”php”]
ls -l /usr/lib/jvm
[/cc]
命令输出:
[cc lang=”php”]
[root@devops Desktop]# ls -l /usr/lib/jvm
total 8
lrwxrwxrwx. 1 root root 26 Dec 14 04:13 java -> /etc/alternatives/java_sdk
drwxr-xr-x. 3 root root 4096 Nov 26 14:42 java-1.6.0-openjdk-1.6.0.0.x86_64
lrwxrwxrwx. 1 root root 32 Dec 14 04:13 java-1.7.0 -> /etc/alternatives/java_sdk_1.7.0
drwxr-xr-x. 7 root root 4096 Dec 14 04:13 java-1.7.0-openjdk-1.7.0.71.x86_64
lrwxrwxrwx. 1 root root 34 Dec 14 04:13 java-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.71.x86_64
lrwxrwxrwx. 1 root root 34 Dec 14 04:13 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root 21 Dec 14 04:12 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 27 Nov 26 14:42 jre-1.6.0 -> /etc/alternatives/jre_1.6.0
lrwxrwxrwx. 1 root root 37 Nov 26 14:42 jre-1.6.0-openjdk.x86_64 -> java-1.6.0-openjdk-1.6.0.0.x86_64/jre
lrwxrwxrwx. 1 root root 27 Dec 14 04:12 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx. 1 root root 38 Dec 14 04:12 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.71.x86_64/jre
lrwxrwxrwx. 1 root root 29 Dec 14 04:12 jre-openjdk -> /etc/alternatives/jre_openjdk
[/cc]
使用export命令设置JAVA_HOME的变量值为包含”bin/java”可执行文件的目录
[cc lang=”php”]
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
[/cc]
java环境变量就配置完成了。
下面我们来测试执行一个java程序。
创建一个名为test.java的文件,加入下面的代码:
[cc lang=”java”]
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World! osetc.com”);
}
}
[/cc]
编译并执行test.java程序:
[cc lang=”php”]
javac HelloWorld.java
java HelloWorld
[/cc]
程序输出:
[cc lang=”php”]
Hello, World!
[/cc]

Leave a Reply