Java是一種安全,穩定且廣爲人知的通用編程語言和計算技術平臺,具備許多相互關聯的功能。html
要運行基於Java的應用程序,必須在服務器上安裝Java。您一般須要Java Runtime Environment(JRE),這是用於在Linux機器上運行Java應用程序的軟件組件的集合。java
若是要開發Java的軟件應用程序,則須要安裝Oracle Java開發工具包(JDK),該工具包帶有一個完整的JRE軟件包,其中包含用於開發,調試和監視Java應用程序的工具,它是Oracle支持的Java SE(標準版)版本。linux
注意:若是您正在尋找開放源代碼和免費的JDK版本,請安裝OpenJDK,它在GPL許可下提供與Oracle JDK相同的功能和性能。shell
在撰寫本文時,OpenJDK 11是要使用默認存儲庫中的如下命令安裝的Java的當前LTS版本:編程
若是須要編譯器,也要安裝[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
[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: