CentOS 使用yum工具安装JAVA SDK

CentOS osetc 4356℃ 0评论

如何在centos系统里安装JAVA JDK 运行时环境呢?本文将会介绍通过使用yum命令安装JAVA SDK 开发环境。
CentOS 6.x 和5.x 版本默认都安装了openJDK运行时环境。这是一个开源的java版本。

java
CentOS 默认安装的JAVA 软件包包括:

1
2
java-1.7.0-openjdk - OpenJDK 运行时环境
java-1.7.0-openjdk-devel - OpenJDK 开发环境
1
2
使用下面的命令查看yum软件库里可用的JDK 软件包:
yum search java | grep -i --color JDK

命令输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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

CentOS 安装 Java SDK 开发包
在命令行下通过yum命令安装JAVA SDK:

1
yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel

命令输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
[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) <centos-6-key@centos.org>
 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!

CentOS设置 JAVA_HOME 环境变量
java SDK包默认会安装在“/usr/lib/jvm” 目录下:

1
ls -l /usr/lib/jvm

命令输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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

使用export命令设置JAVA_HOME的变量值为包含”bin/java”可执行文件的目录

1
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64

java环境变量就配置完成了。
下面我们来测试执行一个java程序。
创建一个名为test.java的文件,加入下面的代码:

1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World! osetc.com");
    }
}

编译并执行test.java程序:

1
2
javac HelloWorld.java
java HelloWorld

程序输出:

1
Hello, World!

转载请注明:osetc.com » CentOS 使用yum工具安装JAVA SDK

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址