在CentOS中如何安装Java SDK

时间:2019-11-20 08:52:41  来源:igfitidea点击:

在CentOS 6上如何安装JDK?
CentOS 6.x和5.x附带了OpenJDK,它是Java平台的开源实现。

在CentOS Linux中,JAVA软件包名称

  • java-1.7.0-openjdkOpenJDK运行时环境
  • java-1.7.0-openjdk-develOpenJDK开发环境

检查系统存储库可用的java包

# yum search java | grep -i --color JDK

输出示例:

[root@iZ8vbcuf25doccbi4t0prwZ ~]# yum search java | grep -i --color JDK
java-11-openjdk.x86_64 : OpenJDK Runtime Environment 11
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment 8
java-11-openjdk-src.x86_64 : OpenJDK Source Bundle 11
java-latest-openjdk.x86_64 : OpenJDK Runtime Environment 15
java-11-openjdk-demo.x86_64 : OpenJDK Demos 11
java-11-openjdk-devel.x86_64 : OpenJDK Development Environment 11
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-latest-openjdk-src.x86_64 : OpenJDK Source Bundle 15
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-11-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 11
java-latest-openjdk-demo.x86_64 : OpenJDK Demos 15
java-latest-openjdk-devel.x86_64 : OpenJDK Development Environment 15
java-latest-openjdk-jmods.x86_64 : JMods for OpenJDK 15
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 8
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in
java-11-openjdk-static-libs.x86_64 : OpenJDK libraries for static linking 11
java-latest-openjdk-javadoc.x86_64 : OpenJDK 15 API documentation
java-latest-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 15
java-latest-openjdk-fastdebug.x86_64 : OpenJDK Runtime Environment 15
java-latest-openjdk-slowdebug.x86_64 : OpenJDK Runtime Environment 15 with full
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed
java-latest-openjdk-javadoc-zip.x86_64 : OpenJDK 15 API documentation compressed
java-latest-openjdk-static-libs.x86_64 : OpenJDK libraries for static linking 15
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK 8 accessibility connector
java-latest-openjdk-src-fastdebug.x86_64 : OpenJDK Source Bundle 15
java-latest-openjdk-src-slowdebug.x86_64 : OpenJDK Source Bundle 15 for packages
java-latest-openjdk-demo-fastdebug.x86_64 : OpenJDK Demos 15 %{fastdebug_on}
java-latest-openjdk-demo-slowdebug.x86_64 : OpenJDK Demos 15 with full debug on
java-latest-openjdk-devel-fastdebug.x86_64 : OpenJDK Development Environment 15
java-latest-openjdk-devel-slowdebug.x86_64 : OpenJDK Development Environment 15
java-latest-openjdk-jmods-fastdebug.x86_64 : JMods for OpenJDK 15
java-latest-openjdk-jmods-slowdebug.x86_64 : JMods for OpenJDK 15 with full
java-latest-openjdk-headless-fastdebug.x86_64 : OpenJDK Runtime Environment
java-latest-openjdk-headless-slowdebug.x86_64 : OpenJDK Runtime Environment with
java-latest-openjdk-static-libs-fastdebug.x86_64 : OpenJDK libraries for static
java-latest-openjdk-static-libs-slowdebug.x86_64 : OpenJDK libraries for static
icedtea-web.noarch : Additional Java components for OpenJDK - Java browser
[root@iZ8vbcuf25doccbi4t0prwZ ~]# 

在CentOS Linux上安装Java SDK

在Linux 上安装OpenJDK:

# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel

在CentOS Linux上设置JAVA_HOME环境变量

RHEL和CentOS Linux默认将OpenJDK安装到/usr/lib/jvm /目录中:

# ls -l /usr/lib/jvm/

使用export命令设置JAVA_HOM变量:

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

添加到shell配置文件最后面,例如$HOME/.bashrc,/etc/bashrc

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

测试

检查java是否可用

$ javac --version
$ java --version

使用程序测试

创建一个名为HelloWorld.java的程序

$ vi HelloWorld.java

代码如下

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

编译并运行:

$ javac HelloWorld.java
$ java HelloWorld