1.在CentOS中安装openjdk
在Centos中,可以使用 yum 仓库安装 openjdk
[root@centos8 ~]#dnf -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel
[root@centos8 ~]#java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
[root@centos8 ~]#which java
/usr/bin/java
[root@centos8 ~]#ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Feb 8 20:03 /usr/bin/java -> /etc/alternatives/java
[root@centos8 ~]#ll /etc/alternatives/java
lrwxrwxrwx 1 root root 73 Feb 8 20:03 /etc/alternatives/java ->
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre/bin/java
[root@centos8 ~]#rpm -qf /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre/bin/java
java-1.8.0-openjdk-headless-1.8.0.232.b09-0.el8_0.x86_64
直接yum安装不能使用javac编译,需要安装devel版
[root@centos8 ~]#dnf -y install java-1.8.0-openjdk-devel
[root@centos8 ~]#cat Hello.java
class Hello{
public static void main(String[] args)
{
System.out.println("hello, world");
}
}
#编译成字节码
[root@centos8 ~]#javac Hello.java
[root@centos8 ~]#ll Hello.*
-rw-r--r-- 1 root root 416 Oct 24 13:00 Hello.class
-rw-r--r-- 1 root root 130 Aug 22 23:38 Hello.java
[root@centos8 ~]#file Hello.class
Hello.class: compiled Java class data, version 52.0 (Java 1.8)
#运行java程序
[root@centos8 ~]#java Hello
hello, world
2.在Ubuntu中安装openjdk
[root@ubuntu1804 ~]#apt update
[root@ubuntu1804 ~]#apt -y install openjdk-8-jdk
[root@ubuntu1804 ~]#java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-8u242-b08-0ubuntu3~18.04-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
root@ubuntu2004:~# apt -y install openjdk-11-jdk
root@ubuntu2004:~# java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)