One place for hosting & domains

      How To Install Java Development Kit 11 on CentOS 8


      Updated by Linode Contributed by Rajakavitha Kodhandapani

      Java is one of the world’s most popular programming languages. Software written in Java can be compiled and run on any system, making Java a versatile platform that can be used to create anything from software to basic web applications. This guide will show you how to install the Open Java Development Kit (OpenJDK) 11 on CentOS 8.

      OpenJDK is the free and open-source implementation of the Oracle Java Standard Edition (Java SE) Development Kit. OpenJDK and Java SE are equivalent JDKs that include a Java runtime environment (JRE) and tools for developing and compiling Java applications.

      While there are many available versions of OpenJDK, version 11 is the latest Long-Term-Support (LTS) release as of the time of this guide’s publication. For this reason, OpenJDK 11 is the recommended version for developing production applications.

      Before You Begin

      1. Familiarize yourself with our Getting Started guide and complete the steps for connecting to your Linode with SSH and setting your Linode’s hostname and timezone.

      2. Complete the sections of our guide on Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services. This guide will use sudo commands wherever possible, which should be run by a limited, non-root user on your Linode.

      3. Ensure your system is up-to-date:

        sudo yum update
        

      Install OpenJDK

      1. Install the OpenJDK 11 development kit, which includes OpenJRE 11:

        sudo yum install java-11-openjdk-devel
        

        Alternatively, if you simply want to run Java applications that you have already downloaded, you can choose to only install OpenJRE 11:

        sudo yum install java-11-openjdk
        

        Note

        While you can run Java applications directly with the JRE, your applications will be compiled every time they are executed. This is generally slower than running applications that have already been compiled into Java bytecode, and may not be suitable if you plan to execute applications many times.

      2. Check the version of the JRE to verify that it has been properly installed:

        java -version
        

        As of the time of this publication, this command should return:

          
        openjdk version "11.0.7" 2020-04-14 LTS
        OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
        OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
        
        
      3. If you have chosen to install the full OpenJDK development kit, check the version of the compiler as well:

        javac -version
        

        As of the time of this publication, this command should return:

          
        javac 11.0.7
        
        

      Set Environment Variables

      This section will instruct you on how to set the JAVA_HOME and PATH environment variables to help ensure that your Java applications will run without issue.

      1. Open the ~/.bashrc startup file using the text editor of your choice and add the following definitions at the end of the file:

        ~/.bashrc
        1
        2
        3
        
        # [...]
        export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
        export PATH=$PATH:$JAVA_HOME/bin

        Note

        If you are using a shell other than Bash, such as Zsh, you may need to add these lines in a different startup file instead. In the case of Zsh, this would be the ~/.zshrc file.
      2. Save the changes and exit your text editor.

      3. Reload the ~/.bashrc file:

        source ~/.bashrc
        
      4. Verify that the JAVA_HOME and PATH variables were set correctly:

        echo $JAVA_HOME
        echo $PATH
        

        The JAVA_HOME variable should be set to the directory that contains your OpenJDK installation, and the PATH variable should include the directory that contains the OpenJDK binary files.

      Test the Java Installation (Optional)

      To test your Java installation, write a sample HelloWorld Java application and run it with the JRE.

      1. Open a text editor and add the following lines in a file labeled HelloWorld.java to create a simple function that prints “Hello Java World!”:

        HelloWorld.java
        1
        2
        3
        4
        5
        
        public class HelloWorld {
            public static void main(String[] args) {
                System.out.println("Hello Java World!");
            }
        }
      2. Run the application using the JRE:

        java HelloWorld.java
        

        If the installation has been successful, the output will be:

          
        Hello Java World!
        
        

      If you have installed the full OpenJDK development kit, you can compile your application into a bytecode class file prior to running it for faster execution time.

      1. Compile the application you have written:

        javac HelloWorld.java
        
      2. Confirm that the HelloWorld.class file was written to your current directory:

        ls -l HelloWorld.class
        
      3. Run the compiled HelloWorld function using the JRE:

        java HelloWorld
        

        The output should again be:

          
        Hello Java World!
        
        

      This guide is published under a CC BY-ND 4.0 license.



      Source link

      Comment installer Java avec Apt sur Ubuntu 20.04


      [*]

      Introduction

      Java et la machine virtuelle Java (JVM) sont nécessaires pour de nombreux types de logiciels, dont Tomcat, Jetty, Glassfish, Cassandra et Jenkins.

      Dans ce guide, vous installerez différentes versions de l’environnement d’exécution Java (JRE) et du kit de développement Java (JDK) en utilisant apt. Vous installerez OpenJDK ainsi que le JDK officiel d’Oracle. Vous sélectionnerez ensuite la version que vous souhaitez utiliser pour vos projets. Une fois terminé, vous pourrez utiliser le JDK pour développer des logiciels ou utiliser le JRE pour exécuter des logiciels.

      Conditions préalables

      Pour suivre ce tutoriel, vous aurez besoin de :

      Installation du JRE et du JDK par défaut

      L’option la plus simple pour installer Java est d’utiliser la version fournie avec Ubuntu. Par défaut, Ubuntu 20.04 inclut OpenJDK 11, une variante open source du JRE et du JDK.

      Pour installer cette version, mettez d’abord à jour l’index des packages :

      Ensuite, vérifiez si Java est déjà installé :

      Si Java n’est actuellement pas installé, vous verrez la sortie suivante :

      Output

      Command 'java' not found, but can be installed with: sudo apt install default-jre # version 2:1.11-72, or sudo apt install openjdk-11-jre-headless # version 11.0.7+10-3ubuntu1 sudo apt install openjdk-13-jre-headless # version 13.0.3+3-1ubuntu2 sudo apt install openjdk-14-jre-headless # version 14.0.1+7-1ubuntu1 sudo apt install openjdk-8-jre-headless # version 8u252-b09-1ubuntu1

      Exécutez la commande suivante pour installer l’environnement d’exécution Java par défaut (JRE), qui installera le JRE à partir d’OpenJDK 11 :

      • sudo apt install default-jre

      Le JRE vous permettra d’exécuter presque tous les logiciels Java.

      Vérifiez l’installation avec :

      Vous verrez la sortie suivante :

      Output

      openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

      Vous aurez peut-être besoin du kit de développement Java (JDK) en plus du JRE afin de compiler et d’exécuter certains logiciels spécifiques basés sur Java. Pour installer le JDK, exécutez la commande suivante, qui installera également le JRE :

      • sudo apt install default-jdk

      Vérifiez que le JDK est installé en vérifiant la version de javac, le compilateur Java :

      Vous verrez la sortie suivante :

      Output

      javac 11.0.7

      Ensuite, voyons comment installer les JDK et JRE officiels d’Oracle.

      Installation d’Oracle JDK 11

      Le contrat de licence d’Oracle pour Java ne permet pas l’installation automatique via les gestionnaires de packages. Pour installer le JDK d’Oracle, qui est la version officielle distribuée par Oracle, vous devez créer un compte Oracle et télécharger manuellement le JDK afin d’ajouter un nouveau référentiel de packages pour la version que vous souhaitez utiliser. Ensuite, vous pouvez utiliser apt pour l’installer avec l’aide d’un script d’installation tiers.

      La version du JDK d’Oracle que vous devez télécharger doit correspondre à la version du script d’installation. Pour savoir de quelle version vous avez besoin, consultez la page oracle-java11-installer

      Localisez le package pour Focal, comme indiqué dans la figure suivante :

      Package d'installation pour Ubuntu 2.04

      Dans cette image, la version du script est 11.0.7. Dans ce cas, vous aurez besoin d’Oracle JDK 11.0.7. Pas besoin de télécharger quoi que ce soit à partir de cette page. Vous pourrez bientôt télécharger le script d’installation par apt.

      Ensuite, rendez-vous à la page Téléchargement et recherchez la version qui correspond à celle dont vous avez besoin.

      Oracle Java 11

      Cliquez sur le bouton Télécharger JDK. Vous serez redirigé vers un écran affichant les versions disponibles. Cliquez sur le package .tar.gz pour Linux.

      Téléchargement de Linux

      Un écran s’affiche vous demandant d’accepter le contrat de licence Oracle. Cochez la case pour accepter le contrat de licence, puis appuyez sur le bouton Télécharger. Votre téléchargement commencera. Vous aurez peut-être besoin de vous connecter à votre compte Oracle une fois de plus avant que le téléchargement ne démarre.

      Une fois que le fichier aura été téléchargé, vous devez le transférer à votre serveur. Sur votre ordinateur local, téléchargez le fichier sur votre serveur. Sous macOS, Linux ou Windows utilisant le sous-système Windows pour Linux, utilisez la commande scp pour transférer le fichier vers le répertoire personnel de votre utilisateur sammy. La commande suivante suppose que vous avez enregistré le fichier Oracle JDK dans le dossier Téléchargement de votre ordinateur local :

      • scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz sammy@your_server_ip:~

      Une fois le téléchargement du fichier terminé, revenez sur votre serveur et ajoutez le référentiel tiers qui vous aidera à installer Java d’Oracle.

      Installez le package software-properties-common pour ajouter la commande add-apt-repository à votre système :

      • sudo apt install software-properties-common

      Ensuite, importez la clé de signature utilisée pour vérifier le logiciel que vous êtes sur le point d’installer :

      • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

      Vous verrez la sortie suivante :

      Output

      gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported gpg: Total number processed: 1 gpg: imported: 1

      Utilisez ensuite la commande add-apt-repository pour ajouter le référentiel à votre liste de sources de packages :

      • sudo add-apt-repository ppa:linuxuprising/java

      Vous verrez le message suivant :

      Output

      Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian. Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, so a working Internet connection is required. The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java Created for users of https://www.linuxuprising.com/ Installation instructions (with some tips), feedback, suggestions, bug reports etc.: . . . Press [ENTER] to continue or ctrl-c to cancel adding it

      Appuyez sur ENTER pour poursuivre l’installation. Le message no valid OpenPGP data found peut apparaître, mais vous pouvez l’ignorer en toute sécurité.

      Mettez à jour votre liste de packages pour que le nouveau logiciel soit disponible pour l’installation :

      Le programme d’installation recherchera le JDK d’Oracle que vous avez téléchargé dans /var/cache/oracle-jdk11-installer-local.  Créez ce répertoire pour y déplacer l’archive Oracle JDK :

      • sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
      • sudo cp jdk-11.0.7_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

      Pour finir, installez le package :

      • sudo apt install oracle-java11-installer-local

      Le programme d’installation vous demandera tout d’abord d’accepter le contrat de licence d’Oracle. Une fois que vous aurez accepté le contrat, le programme d’installation procèdera à l’extraction du package Java et à son installation.

      Voyons maintenant comment sélectionner la version de Java que vous souhaitez utiliser.

      Gestion de Java

      Vous pouvez avoir plusieurs installations Java sur un même serveur. Vous pouvez configurer la version utilisée par défaut sur la ligne de commande en utilisant la commande update-alternatives.

      • sudo update-alternatives --config java

      Voici à quoi devrait ressembler la sortie si vous avez installé les deux versions de Java de ce tutoriel :

      Output

      There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode Press <enter> to keep the current choice[*], or type selection number:

      Choisissez le numéro associé à la version Java afin de l’utiliser par défaut ou appuyez sur ENTER pour conserver les paramètres actuels.

      Vous pouvez faire cela pour d’autres commandes Java, telles que le compilateur (javac) :

      • sudo update-alternatives --config javac

      Les autres commandes pour lesquelles cette commande peut être exécutée comprennent, sans s’y limiter : keytool, javadoc et jarsigner.

      Définition de la variable d’environnement JAVA_HOME

      De nombreux programmes écrits en Java utilisent la variable d’environnement JAVA_HOME pour déterminer l’emplacement d’installation de Java.

      Pour définir cette variable d’environnement, il faut d’abord déterminer où Java est installé. Utilisez la commande update-alternatives :

      • sudo update-alternatives --config java

      Cette commande montre chaque installation de Java ainsi que son chemin d’installation :

      Output

      There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode Press <enter> to keep the current choice[*], or type selection number:

      Dans ce cas, les chemins d’installation sont les suivants :

      1. OpenJDK 11 se trouve dans /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
      2. Oracle Java se trouve dans /usr/lib/jvm/java-11-oracle/jre/bin/java.

      Copiez le chemin de votre installation souhaitée. Puis, ouvrez /etc/environment en utilisant nano ou votre éditeur de texte préféré :

      • sudo nano /etc/environment

      À la fin de ce fichier, ajoutez la ligne suivante, en veillant à bien remplacer le chemin en surbrillance par le vôtre que vous aurez copié, mais n’incluez pas la partie bin/ du chemin :

      /etc/environment

      JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
      

      La modification de ce fichier définira le chemin JAVA_HOME pour tous les utilisateurs de votre système.

      Enregistrez le fichier et quittez l’éditeur.

      Rechargez maintenant ce fichier pour appliquer les changements à votre session actuelle :

      Vérifiez que la variable d’environnement est définie :

      Vous verrez le chemin que vous venez de définir :

      Output

      /usr/lib/jvm/java-11-openjdk-amd64

      Les autres utilisateurs devront exécuter la commande source /etc/environment, ou se déconnecter et se reconnecter pour appliquer ce paramètre.

      Conclusion

      Dans ce tutoriel, vous avez installé plusieurs versions de Java et appris à les gérer. Vous pouvez désormais installer des logiciels fonctionnant grâce à Java, tels que Tomcat, Jetty, Glassfish, Cassandra ou Jenkins.

      [*]
      [*]Source link

      How To Install Java with Apt on Ubuntu 20.04


      [*]

      Introduction

      Java and the JVM (Java’s virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.

      In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt . You’ll install OpenJDK as well as the official JDK from Oracle. You’ll then select the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK to develop software or use the Java Runtime to run software.

      Prerequisites

      To follow this tutorial, you will need:

      Installing the Default JRE/JDK

      The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 20.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK.

      To install this version, first update the package index:

      Next, check if Java is already installed:

      If Java is not currently installed, you’ll see the following output:

      Output

      Command 'java' not found, but can be installed with: sudo apt install default-jre # version 2:1.11-72, or sudo apt install openjdk-11-jre-headless # version 11.0.7+10-3ubuntu1 sudo apt install openjdk-13-jre-headless # version 13.0.3+3-1ubuntu2 sudo apt install openjdk-14-jre-headless # version 14.0.1+7-1ubuntu1 sudo apt install openjdk-8-jre-headless # version 8u252-b09-1ubuntu1

      Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

      • sudo apt install default-jre

      The JRE will allow you to run almost all Java software.

      Verify the installation with:

      You’ll see the following output:

      Output

      openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

      You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:

      • sudo apt install default-jdk

      Verify that the JDK is installed by checking the version of javac, the Java compiler:

      You’ll see the following output:

      Output

      javac 11.0.7

      Next, let’s look at how to install Oracle’s official JDK and JRE.

      Installing Oracle JDK 11

      Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers. To install the Oracle JDK, which is the official version distributed by Oracle, you must create an Oracle account and manually download the JDK to add a new package repository for the version you’d like to use. Then you can use apt to install it with help from a third party installation script.

      The version of Oracle’s JDK you’ll need to download must match version of the installer script. To find out which version you need, visit the oracle-java11-installer page.

      Locate the package for Focal, as shown in the following figure:

      Installer package for Ubuntu 2.04

      In this image, the version of the script is 11.0.7. In this case, you’ll need Oracle JDK 11.0.7. You don’t need to download anything from this page; you’ll download the installation script through apt shortly.

      Then visit the Downloads page and locate the version that matches the one you need.

      Oracle Java 11

      Click the JDK Download button and you’ll be taken to a screen that shows the versions available. Click the .tar.gz package for Linux.

      Linux download

      You’ll be presented with a screen asking you to accept the Oracle license agreement. Select the checkbox to accept the license agreement and press the Download button. Your download will begin. You may need to log in to your Oracle account one more time before the download starts.

      Once the file has downloaded, you’ll need to transfer it to your server. On your local machine, upload the file to your server. On macOS, Linux, or Windows using the Windows Subsystem for Linux, use the scp command to transfer the file to the home directory of your sammy user. The following command assumes you’ve saved the Oracle JDK file to your local machine’s Downloads folder:

      • scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz sammy@your_server_ip:~

      Once the file upload has completed, return to your server and add the third-party repository that will help you install Oracle’s Java.

      Install the software-properties-common package, which adds the add-apt-repository command to your system:

      • sudo apt install software-properties-common

      Next, import the signing key used to verify the software you’re about to install:

      • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

      You’ll see this output:

      Output

      gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported gpg: Total number processed: 1 gpg: imported: 1

      Then use the add-apt-repository command to add the repo to your list of package sources:

      • sudo add-apt-repository ppa:linuxuprising/java

      You’ll see this message:

      Output

      Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian. Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, so a working Internet connection is required. The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java Created for users of https://www.linuxuprising.com/ Installation instructions (with some tips), feedback, suggestions, bug reports etc.: . . . Press [ENTER] to continue or ctrl-c to cancel adding it

      Press ENTER to continue the installation. You may see a message about no valid OpenPGP data found, but you can safely ignore this.

      Update your package list to make the new software available for installation:

      The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local. Create this directory and move the Oracle JDK archive there:

      • sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
      • sudo cp jdk-11.0.7_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

      Finally, install the package:

      • sudo apt install oracle-java11-installer-local

      The installer will first ask you to accept the Oracle license agreement. Accept the agreement, then the installer will extract the Java package and install it.

      Now let’s look at how to select which version of Java you want to use.

      Managing Java

      You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

      • sudo update-alternatives --config java

      This is what the output would look like if you’ve installed both versions of Java in this tutorial:

      Output

      There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode Press <enter> to keep the current choice[*], or type selection number:

      Choose the number associated with the Java version to use it as the default, or press ENTER to leave the current settings in place.

      You can do this for other Java commands, such as the compiler (javac):

      • sudo update-alternatives --config javac

      Other commands for which this command can be run include, but are not limited to: keytool, javadoc and jarsigner.

      Setting the JAVA_HOME Environment Variable

      Many programs written using Java use the JAVA_HOME environment variable to determine the Java installation location.

      To set this environment variable, first determine where Java is installed. Use the update-alternatives command:

      • sudo update-alternatives --config java

      This command shows each installation of Java along with its installation path:

      Output

      There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode Press <enter> to keep the current choice[*], or type selection number:

      In this case the installation paths are as follows:

      1. OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
      2. Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java.

      Copy the path from your preferred installation. Then open /etc/environment using nano or your favorite text editor:

      • sudo nano /etc/environment

      At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the bin/ portion of the path:

      /etc/environment

      JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
      

      Modifying this file will set the JAVA_HOME path for all users on your system.

      Save the file and exit the editor.

      Now reload this file to apply the changes to your current session:

      Verify that the environment variable is set:

      You’ll see the path you just set:

      Output

      /usr/lib/jvm/java-11-openjdk-amd64

      Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.

      Conclusion

      In this tutorial you installed multiple versions of Java and learned how to manage them. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.

      [*]
      [*]Source link