首页 > 编程语言 > CentOS8.2安装Java 14.0.2的教程详解
2020
12-10

CentOS8.2安装Java 14.0.2的教程详解

Java是一种安全,稳定且广为人知的通用编程语言和计算技术平台,具有许多相互关联的功能。

要运行基于Java的应用程序,必须在服务器上安装Java。您通常需要Java Runtime Environment(JRE),这是用于在Linux机器上运行Java应用程序的软件组件的集合。

如果要开发Java的软件应用程序,则需要安装Oracle Java开发工具包(JDK),该工具包带有一个完整的JRE软件包,其中包含用于开发,调试和监视Java应用程序的工具,它是Oracle支持的Java SE(标准版)版本。

注意:如果您正在寻找开放源代码和免费的JDK版本,请安装OpenJDK,它在GPL许可下提供与Oracle JDK相同的功能和性能。

在撰写本文时,OpenJDK 11是要使用默认存储库中的以下命令安装的Java的当前LTS版本:

[1] 安装[java-11-openjdk]包,

如果需要编译器,也要安装[java-11-openjdk-devel]包。

[root@nfsclient ~]# dnf -y install java-11-openjdk java-11-openjdk-devel
Last metadata expiration check: 0:29:58 ago on Wed 09 Dec 2020 10:25:45 PM CST.
Dependencies resolved.
==============================================================================================================================
 Package              Architecture   Version                    Repository      Size
==============================================================================================================================
Installing:
 java-11-openjdk          x86_64      1:11.0.9.11-0.el8_2              AppStream      251 k
 java-11-openjdk-devel       x86_64      1:11.0.9.11-0.el8_2              AppStream      3.4 M
Installing dependencies:
 copy-jdk-configs          noarch      3.7-1.el8                   AppStream      27 k
 java-11-openjdk-headless      x86_64      1:11.0.9.11-0.el8_2              AppStream      40 M
 javapackages-filesystem      noarch      5.3.0-1.module_el8.0.0+11+5b8c10bd       AppStream      30 k
 lksctp-tools            x86_64      1.0.18-3.el8                  base        100 k
 ttmkfdir              x86_64      3.0.9-54.el8                  AppStream      62 k
 tzdata-java            noarch      2020d-1.el8                  AppStream      190 k
 xorg-x11-fonts-Type1        noarch      7.5-19.el8                   AppStream      522 k
Enabling module streams:
 javapackages-runtime                201801

Transaction Summary
==============================================================================================================================

[root@nfsclient ~]# java --version
openjdk 11.0.9 2020-10-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.9+11-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.9+11-LTS, mixed mode, sharing)

# verify to create test program
[root@nfsclient ~]# cat >/tmp/java_test.java<<EOF
class java_test {
  public static void main(String[] args) {
    System.out.println("Hello Java World !");
  }
}
EOF

[root@dlp ~]# javac java_test.java
[root@dlp ~]# java java_test
Hello Java World !

如果你从CentOS官方存储库中安装了多个版本的Java,你可以用其他的方法来切换它们。

[root@nfsclient ~]# java --version
openjdk 11.0.5 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)
[root@dlp ~]# javac --version
javac 11.0.5

[2]安装 jdk-14.0.2_linux-x64_bin.rpm

[root@nfsclient tmp]# dnf -y install jdk-14.0.2_linux-x64_bin.rpm

[root@nfsclient tmp]# java --version
java 14.0.2 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)

[root@nfsclient tmp]# cat >/etc/profile.d/jdk14.sh <<EOF
export JAVA_HOME=/usr/java/default
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

To use Java Home, source the file.

[root@nfsclient tmp]# source /etc/profile.d/jdk14.sh

测试Java安装。
为了测试我们的Java安装,让我们创建一个简单的html文件。创建一个test.java文件,其内容如下

[root@nfsclient ~]# cat >/tmp/HelloWorld.java<<EOF
public class HelloWorld {
	public static void main(String[] args) {
	System.out.println(“Hello, world”);
	}
}
EOF

[root@nfsclient ~]# cat >/tmp/java_test01.java<<EOF
class java_test {
  public static void main(String[] args) {
    System.out.println("Hello Java World !");
  }
}
EOF

[root@nfsclient ~]#javac java_test01.java
[root@nfsclient ~]# java java_test01
Hello Java World !

如果你从CentOS官方存储库中安装了多个版本的Java,你可以用其他的方法来切换它们。

[root@nfsclient tmp]# alternatives --config java

There are 2 programs which provide 'java'.

 Selection  Command
-----------------------------------------------
  1      java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.9.11-0.el8_2.x86_64/bin/java)
*+ 2      /usr/java/jdk-14.0.2/bin/java

Enter to keep the current selection[+], or type selection number: 2

[root@nfsclient ~]#alternatives --config javac

[root@nfsclient ~]# alternatives --install /usr/bin/javac javac /usr/java/jdk-14.0.2/bin/javac 100
[root@nfsclient tmp]# alternatives --config javac

There are 2 programs which provide 'javac'.

 Selection  Command
-----------------------------------------------
  1      java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.9.11-0.el8_2.x86_64/bin/javac)
*+ 2      /usr/java/jdk-14.0.2/bin/javac

Enter to keep the current selection[+], or type selection number:

到此这篇关于CentOS8.2安装Java 14.0.2的文章就介绍到这了,更多相关CentOS安装Java 14内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧