Java is one of the most popular programming languages, and it can be used to build different types of applications and systems.
There are two different implementations of Java, OpenJDK and Oracle Java. Most of them are the same, except that Oracle Java has some additional commercial features. Oracle Java License only allows non-commercial use, such as personal use or developer use. OpenJDK is an open source implementation of the Java platform.
The default software source repository of CentOS 8 contains two long-term support versions of Java, Java 8 and Java 11.
In this guide, we will explain how to install one or more Java (OpenJDK) versions on CentOS 8 and how to set the default Java version.
It is usually recommended to install the latest Java long-term support version (JDK 11). Some Java-based applications may require a specific version of Java, so you may need to read the application documentation.
To install OpenJDK 11 on CentOS 8, run the following command as root or another user with sudo privileges:
sudo yum install java-11-openjdk-devel
Once the installation is complete, you can verify by checking the Java version number.
java -version
The output should look like this:
openjdk version "11.0.4"2019-07-16 LTS
OpenJDK Runtime Environment 18.9(build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM 18.9(build 11.0.4+11-LTS, mixed mode, sharing)
that's it! You have successfully installed Java on CentOS 8 system.
CentOS 8 also supports OpenJDK minimum Java runtime environment, which can execute applications without GUI interface (no keyboard, no mouse, and no display system support). This version is suitable for server applications because it has fewer dependencies and uses fewer server system resources.
To install the OpenJDK 11 headless version, enter:
sudo yum install java-11-openjdk-headless
If you have installed java-11-openjdk-devel
, the headless package will be installed as a dependent package.
Java 8, the previous version of Java LTS, is still supported and widely used. If your application requires Java 8, you should enter the following command to install it:
sudo yum install java-1.8.0-openjdk-devel
Verify it by checking the Java version:
java -version
The output should look like this:
openjdk version "1.8.0_222"
OpenJDK Runtime Environment(build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM(build 25.222-b10, mixed mode)
To install the minimal java runtime environment, please install the java-1.8.0-openjdk-headless
package.
If you have installed multiple Java versions on your CentOS system, you can use alternatives to set the default Java version.
To check what the default Java version is, type:
java -version
If you want to change the default version, use the alternatives
command:
sudo alternatives --config java
The output should look like this:
There are 2 programs which provide 'java'.
Selection Command
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 java-11-openjdk.x86_64(/usr/lib/jvm/java-11-openjdk-11.0.4.11-0.el8_0.x86_64/bin/java)*+2 java-1.8.0-openjdk.x86_64(/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el8_0.x86_64/jre/bin/java)
Enter to keep the current selection[+], or type selection number:
All installed Java versions are printed out on the screen. Enter the number you want to set as the default version, and press Enter.
You may want to change the default javac
version:
sudo alternatives --config java
javac
is a command tool used to compile Java programs.
JAVA_HOME
**environment variables## The JAVA_HOME
environment variable is used in some Java applications. It is used to indicate the location of the Java installation and to specify the Java version used.
To set the JAVA_HOME
environment variable for each user, add it to the ~/.bashrc
file, or any other configuration file that will be loaded when the user logs in. For system-level settings, use a script in the /etc/profile.d
directory:
If you want to set JAVA_HOME
to OpenJDK 8, add the following line to the bottom of the file: /etc/profile.d/java.sh
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"
If you want this modification to work in your current session, you can log out of the system and log in again, or use the following source
command:
source /etc/profile.d/java.sh
Verify that the JAVA_HOME
environment variable is set correctly:
echo $JAVA_HOME
The output shows the path to the Java installation:
/usr/lib/jvm/java-1.8.0-openjdk
You can also set JAVA_HOME
in the application configuration file, in the systemd unit file, or on the command line when starting the program.
For example, if you want Mavan to use Java 8, you can enter:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk mvn --version
Output:
...
Java version:1.8.0_222, vendor: Oracle Corporation, runtime:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el8_0.x86_64/jre
...
CentOS 8 supports two main Java versions, Java 8 and Java 11, both of which can be installed using the yum
package management tool.
Recommended Posts