未加星标

How to install Java 8 an 11 on Red Hat Enterprise Linux 8 Beta

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

How to install Java 8 an 11 on Red Hat Enterprise Linux 8 Beta

With Red Hat Enterprise linux (RHEL) 8 Beta, two major versions of Java will be supported: Java 8 and Java 11. In this article, I’ll refer to Java 8 as JDK (Java Development Kit) 8 since we are focusing on the development aspect of using Java. JDK 8 and JDK 11 refer to Red Hat builds of OpenJDK 8 and OpenJDK 11 respectively. Through this article, you’ll learn how to install and run simple Java applications on RHEL 8 Beta, how to switch between two parallel installed major JDK versions via alternatives and how to select one of the two JDKs on a per-application basis.

TL; DR

To install JDK 8, use: (If you didn’t select Make this user administrator during install see this article toenable sudo on RHEL)

$ sudo yum install java-1.8.0-openjdk-devel

Then run Java “Hello World” as follows:

$ cat > HelloWorld.java <<HELLO
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HELLO
$ javac HelloWorld.java && java HelloWorld
Hello World!

To instal JDK 11, use:

$ sudo yum install java-11-openjdk-devel

Then run Java “Hello World” as follows:

$ cat > HelloWorld.java <<HELLO
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HELLO
$ /usr/lib/jvm/java-11-openjdk/bin/java HelloWorld.java
Hello World!

Yes, with JDK 11 you can directly run Java source files. The compilation step is handled for you.

Watch the Video

If you prefer to watch a short 4 minute demo video, here it is:

The Longer Version…

Let’s consider we have a freshly commissioned Red Hat Enterprise Linux 8 Beta machine and we’d like to use it for running Java applications.


How to install Java 8 an 11 on Red Hat Enterprise Linux 8 Beta
Finding available Java packages

In order to figure out which RPM packages to install, we can ask the packaging system which ones provide the java binary:

$ yum provides \*/bin/java

This command tells us that packages java-1.8.0-openjdk-headless and java-11-openjdk-headless both provide the java binary. For the purpose of this article, we’re interested in the development kits, so we’ll install the -devel sub-packages instead. The -devel packages will pull in -headless packages as a dependency. If you already know that RHEL packages are OpenJDK builds, yum list available might be useful too:

$ yum list available \*openjdk\*

For the purpose of this article, we are going to install JDK 8 and JDK 11 in parallel , and also install maven:

$ sudo yum install java-1.8.0-openjdk-devel java-11-openjdk-devel maven Switching Java Versions

In the previous step, we installed JDK 8 and JDK 11 in parallel. At this point in time, JDK 8 is the main JDK on RHEL 8 Beta. That’s why you get this output when running java -version on a fresh RHEL 8 Beta install:

$ java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

There are two ways to select the Java version you want:

Switch java and javac binaries system wide via alternatives. This approach requires root privileges . Select the JDK on a per-application basis by setting JAVA_HOME Selecting Java Versions with Alternatives

java and javac binaries on RHEL 8 beta are managed by the alternatives system. This means a system administrator can switch the system java (or javac ) to be something other than the default, JDK 8. The alternatives system uses priorities in order to determine which JDK should be available via /usr/bin/java . JDK 8 has been given a higher priority on RHEL-8 Beta than JDK 11. But we are getting ahead of ourselves. First, lets see which binaries are managed by alternatives:

$ alternatives --list

We see that java and javac are managed by alternatives. Next, we are going to switch to JDK 11, using the alternatives --config command:

$ sudo alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-8.el8.x86_64/jre/bin/java)
2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-4.el8.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number: 2

This will switch the system java binary to JDK 11. We do the same for javac since java and javac are independently managed. There is no need to switch anything else as every other JDK binary will switch either with the java or the javac binary:

$ sudo alternatives --config javac
There are 2 programs which provide 'javac'.
Selection Command
-----------------------------------------------
*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-8.el8.x86_64/jre/bin/javac)
2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-4.el8.x86_64/bin/javac)
Enter to keep the current selection[+], or type selection number: 2 Switching alternatives non-interactively

Here is an approach to switch to JDK 11, via alternatives, using non-interactive means, which comes in handy if you need to script it:

$ JAVA_11=$(alternatives --display java | grep 'family java-11-openjdk' | cut -d' ' -f1)
$ sudo alternatives --set java $JAVA_11

Similarly, switching to JDK 8 via alternatives by non-interactive means:

$ JAVA_8=$(alternatives --display java | grep 'family java-1.8.0-openjdk' | cut -d' ' -f1)
$ sudo alternatives --set java $JAVA_8

A similar approach can be followed for javac .

Selecting Java Versions by Setting JAVA_HOME

Many applications support using the JAVA_HOME environment variable, as a way to specify which JDK should be used to run the application. The examples below demonstrate this usage when running maven. This approach is handy if you don’t have root privileges, but both JDKs are already installed on your system.

Selecting JDK 8:

$ JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk mvn --version
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-9.el8.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-2.el8.x86_64", arch: "amd64", family: "unix"

Selecting JDK 11:

$ JAVA_HOME=/usr/lib/jvm/java-11-openjdk mvn --version
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-11.0.1.13-6.el8.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-2.el8.x86_64", arch: "amd64", family: "unix" This feature also comes in handy if you have multiple minor JDK versions installed as

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

代码区博客精选文章
分页:12
转载请注明
本文标题:How to install Java 8 an 11 on Red Hat Enterprise Linux 8 Beta
本站链接:https://www.codesec.net/view/621272.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(52)