Java is a programming language having almost similar syntax to C++, intended to have minimum dependencies and let the developer write once, run anywhere (WORA)

To run a Java program, a Java Runtime Environment (JRE) is needed to be installed, not to be confused with Java Development Kit (JDK), which is needed only to compile a Java program.

Latest Java version offered by official repository may differ depending on distribution you are having. At the point of this article is written, CentOS has Java 11 as latest version while the latest version from official Java is Java 19.

Steps to install Java Development Kit (JDK) on CentOS ===== Steps to install Java Development Kit (JDK) on CentOS & Red Hat: ===== Red Hat:

  1. Launch terminal.
  2. Install OpenJDK JDK from official repository
    [user@centos-host ~]$ sudo yum install java-11-openjdk-devel.x86_64 --assumeyes
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.ipserverone.com
     * extras: mirror.gi.co.id
     * updates: mirrors.gbnetwork.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package java-11-openjdk-devel.x86_64 1:11.0.16.1.1-1.el7_9 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =========================================================================================================================
     Package                            Arch                Version                               Repository            Size
    =========================================================================================================================
    Installing:
     java-11-openjdk-devel              x86_64              1:11.0.16.1.1-1.el7_9                 updates              3.4 M
    
    Transaction Summary
    =========================================================================================================================
    Install  1 Package
    
    Total download size: 3.4 M
    Installed size: 5.2 M
    Downloading packages:
    java-11-openjdk-devel-11.0.16.1.1-1.el7_9.x86_64.rpm                                              | 3.4 MB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : 1:java-11-openjdk-devel-11.0.16.1.1-1.el7_9.x86_64                                                    1/1 
      Verifying  : 1:java-11-openjdk-devel-11.0.16.1.1-1.el7_9.x86_64                                                    1/1 
    
    Installed:
      java-11-openjdk-devel.x86_64 1:11.0.16.1.1-1.el7_9                                                                     
    
    Complete!
    [user@centos-host ~]$ 
  3. Change Java default version accordingly
    [user@centos-host ~]$ sudo alternatives --config javac
    
    There is 2 program that provides 'javac'.
    
      Selection    Command
    -----------------------------------------------
    *+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-2.el7_9.x86_64/bin/javac)
       2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.17.0.8-2.el7_9.x86_64/bin/javac)
    
    Enter to keep the current selection[+], or type selection number:
  4. Check JDK version to verify installation.
    [user@centos-host ~]$ javac --version
    javac 11.0.16.1
    [user@centos-host ~]$ 

Steps to install Java Development Kit (JDK) on Fedora:

  1. Launch terminal.
  2. Install OpenJDK JDK from official repository
    [user@fedora-host ~]$ sudo dnf install java-17-openjdk-devel --assumeyes
    Last metadata expiration check: 0:09:37 ago on Mon 31 Oct 2022 09:55:50 AM +08.
    Dependencies resolved.
    ================================================================================
     Package                   Arch       Version                 Repository   Size
    ================================================================================
    Installing:
     java-17-openjdk-devel     x86_64     1:17.0.4.1.1-1.fc36     updates     4.7 M
    Installing dependencies:
     java-17-openjdk           x86_64     1:17.0.4.1.1-1.fc36     updates     229 k
    
    Transaction Summary
    ================================================================================
    Install  2 Packages
    
    Total download size: 4.9 M
    Installed size: 9.6 M
    Downloading Packages:
    (1/2): java-17-openjdk-17.0.4.1.1-1.fc36.x86_64 426 kB/s | 229 kB     00:00    
    (2/2): java-17-openjdk-devel-17.0.4.1.1-1.fc36. 3.2 MB/s | 4.7 MB     00:01    
    --------------------------------------------------------------------------------
    Total                                           1.7 MB/s | 4.9 MB     00:02     
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Preparing        :                                                        1/1 
      Installing       : java-17-openjdk-1:17.0.4.1.1-1.fc36.x86_64             1/2 
      Running scriptlet: java-17-openjdk-1:17.0.4.1.1-1.fc36.x86_64             1/2 
      Installing       : java-17-openjdk-devel-1:17.0.4.1.1-1.fc36.x86_64       2/2 
      Running scriptlet: java-17-openjdk-devel-1:17.0.4.1.1-1.fc36.x86_64       2/2 
      Running scriptlet: java-17-openjdk-1:17.0.4.1.1-1.fc36.x86_64             2/2 
      Running scriptlet: java-17-openjdk-devel-1:17.0.4.1.1-1.fc36.x86_64       2/2 
      Verifying        : java-17-openjdk-1:17.0.4.1.1-1.fc36.x86_64             1/2 
      Verifying        : java-17-openjdk-devel-1:17.0.4.1.1-1.fc36.x86_64       2/2 
    
    Installed:
      java-17-openjdk-1:17.0.4.1.1-1.fc36.x86_64                                    
      java-17-openjdk-devel-1:17.0.4.1.1-1.fc36.x86_64                              
    
    Complete!
    [user@fedora-host ~]$ 
  3. Change Java default version accordingly
    [user@fedora-host ~]$ sudo update-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.16.1.1-1.fc36.x86_64/bin/javac)
    *  2           java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.4.1.1-1.fc36.x86_64/bin/javac)
    
    Enter to keep the current selection[+], or type selection number: 
  4. Check JDK version to verify installation.
    [user@fedora-host ~]$ javac --version
    javac 17.0.4.1
Discuss the article:

Comment anonymously. Login not required.