Installing Java on the Linux operating system

The author's operating environment:

CentOS Stream 8 x86_64

Java 11.0.12

  1. A Linux operating system must be installed before proceeding with this tutorial.This Linux operating system can be installed in an emulator or on a real computer.For this, see my other blogs:

    VMware Workstation Pro 16 Installation Tutorial:
    https://blog.csdn.net/wangpaiblog/article/details/112152218?spm=1001.2014.3001.5502 Installation tutorial for the Linux operating system CentOS Stream:
    https://blog.csdn.net/wangpaiblog/article/details/119792728?spm=1001.2014.3001.5502

    For the convenience of adding installation files to the Linux operating system, it is best to use a terminal control software, such as Xshell, on a Windows operating system.For this, you can see that...

  2. There are several ways to install Java, such as offline, online, and installation-free.This article mainly introduces the offline installation method.Offline installation requires downloading the installation package yourself first, but since there are a wide variety of Linux operating systems, the first thing you need to know is the processor architecture information for installing the Linux operating system.With CentOS Stream 8 x86_64 For example, enter the following command:

    [root@localhost ~]# cat /proc/version
    Linux version 4.18.0-326.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC)) #1 SMP Wed Jul 28 21:21:05 UTC 2021
    

    As you can see, the key information above is x86_64.Keep this in mind, and you'll use it later.

  3. There are two main types of PMS tools for Linux.

    For Red Hat-based distributions, the PMS tool is rpm and the corresponding installation command is yum.

    For Debian-based distributions, the PMS tool is dpkg and the corresponding installation command is aptitude.

    Author's Operating System CentOS Stream 8 x86_64 belongs to Red Hat and will use an installation package suffixed with rpm.If the operating system used by the reader is Ubuntu, you need to choose an installation package suffixed with deb.If the reader does not know how to tell, enter the following command to verify:

    [root@localhost xshell_win10]# yum
    [...A long list of captions...]
    

    If you enter yum, a large list of instructions appears indicating that you should use an installation package suffixed with rpm.If the prompt Yum command does not exist, the installation package suffixed with deb should be used.Of course, the reader can also enter the command aptitude for further validation.

  4. Installing Java means installing the JDK.There are three major versions of Java, the Java SE chosen here.For differences between these versions, see my other blog: Differences between Java SE, JDK, Java EE, Java Framework, Java ME:
    https://blog.csdn.net/wangpaiblog/article/details/112342415

    In addition, many versions of JDK have been replaced, including LTS and non-LTS versions.LTS refers to Long Term Support.There are no more explanations for this concept, and readers can simply interpret them as stable and up-to-date.

    (The LTS version of Java currently has: Java 8, Java 11, Java 17.Website: https://www.oracle.com/java/technologies/java-se-support-roadmap.html )

    The author's opinion is that it is best to download the latest version of LTS.But for some new Java learners, there is little difference in which version to download, because the difference is not reflected in simple Java programs.When writing this tutorial, I chose Java 11.

  5. Go to the Oracle website to download the JDK installation package.There are many Java versions to choose from.Website: https://www.oracle.com/java/technologies/javase-downloads.html

    Because it is a foreign website, the website may load slowly.Some browsers may disable some controls on this site, so if you find that some interface options are missing from the site, you can try a different browser.

    The detailed operation flow is shown in the figure (please click on the red circle part along the picture with the mouse):

    (Now you need to register an Oracle account to download, please note.)



  6. Here, you can see six download options.For the author's operating system, as explained earlier, the Linux x64 RPM Package should be chosen.In addition, Linux x64 Compressed Archive is an install-free version of Java.If readers like to download an install-free version, they need to download it.

  7. Once the download is complete, you can use the Xshell to place the installation package on your Linux operating system.For installing Java using an installation package, it doesn't matter which directory the installation package is placed in.Assuming that the installation package has been placed in the current directory, you can complete the installation by typing the following commands and some foolish actions:

    sudo yum localinstall jdk-11.0.12_linux-x64_bin.rpm

    (Note: Although this is an offline installation package, the network is not completely required for installation.At the initial installation, the operating system may first update the software repository, which requires networking.)

    Download process example:

    [root@localhost JDK 11 Installation package]# sudo yum localinstall jdk-11.0.12_linux-x64_bin.rpm
    CentOS Stream 8 - AppStream                                                                                                                                281 kB/s |  13 MB     00:49    
    CentOS Stream 8 - BaseOS                                                                                                                                   508 kB/s | 9.5 MB     00:19    
    CentOS Stream 8 - Extras                                                                                                                                   515  B/s |  15 kB     00:29    
    Dependency resolution.
    ===========================================================================================================================================================================================
     software package                                       Framework                                    Edition                                             Warehouse                                           Size
    ===========================================================================================================================================================================================
    install:
     jdk-11.0.12                                  x86_64                                  2000:11.0.12-ga                                  @commandline                                  156 M
    
    Transaction Summary
    ===========================================================================================================================================================================================
    Install 1 Package
    
    Total: 156 M
     Installation size: 294 M
     Are you sure?[y/N]:  y
     Download the package:
    Run Transaction Check
     Transaction check succeeded.
    Run Transaction Tests
     Transaction test succeeded.
    Run Transaction
      Preparing  :                                                                                                                                                                            1/1 
      install    : jdk-11.0.12-2000:11.0.12-ga.x86_64                                                                                                                                         1/1 
      Run script: jdk-11.0.12-2000:11.0.12-ga.x86_64                                                                                                                                         1/1 
      Verification    : jdk-11.0.12-2000:11.0.12-ga.x86_64                                                                                                                                         1/1 
    
    already installed:
      jdk-11.0.12-2000:11.0.12-ga.x86_64                                                                                                                                                       
    
    Complete!
    
  8. Verify that the installation was successful.Enter the following commands:

    yum list installed jdk*
    java --version

    If the Java versions just installed appear, the installation is successful.

    Command output result example:

    [root@localhost ~]# yum list installed jdk*
    Installed Packages
    jdk-11.0.12.x86_64                                                                      2000:11.0.12-ga                                                                       @@commandline
    [root@localhost ~]# java --version
    java 11.0.12 2021-07-20 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)
    
  9. Although the Java installation is complete, many JDK-dependent software relies on environment variables in the operating system for Java.One lazy way to set Java environment variables on Linux is to add them to the file/etc/profile.The reason for being lazy is that once you upgrade or reinstall the Linux operating system, the file will be reset.The best way is to place your own environment variables in a series of files prefixed with.bash under $HOME/.But sometimes the author is lazy.

  10. There are many ways to append content to text files, either by using Xftp or by using the VIM command: vim/etc/profile.Before you do that, you need to know where Java was installed when the previous installation package was automatically installed.You can use the command which java to learn the installation path for Java:

    [root@localhost ~]# which java
    /usr/bin/java
    
  11. What you need to append at the end of the file/etc/profile is:

    export JAVA_HOME=/usr/bin/java
    export PATH=$PATH:$JAVA_HOME/bin
    

    Where JAVA_The content of the HOME is the installation path for Java as previously viewed.

  12. Now you need to verify that the environment variable was set successfully, but don't rush to verify it, because the file/etc/profile will not be read until Linux starts, so you need to restart it before the previous settings take effect.There are many ways to restart, or you can use the command reboot.

  13. After restart, enter the following command, and if the previous settings are output, the previous settings are successful.

    echo $JAVA_HOME
    echo $PATH

    Command output result example:

    [root@localhost ~]# echo $JAVA_HOME
    /usr/bin/java
    [root@localhost ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/bin/java/bin:/root/bin
    
  14. Introduction to other Java installation methods:

    • Online installation: You need to set up Ali Cloud Mirror first, then output a list of installable Java versions, then select a version from the installation, and finally set the environment variables as described in this tutorial.
    • Installation-free method: You need to download the zip package Linux x64 Compressed Archive mentioned earlier, specify a directory to unzip the zip package, and finally set the environment variable as described in this tutorial.

Tags: Java Linux CentOS

Posted on Fri, 03 Sep 2021 21:54:16 -0400 by urbansmooth