Installing Java on Linux Cloud Servers

2020-02-24 By Aaron 2907 Views linux java development
0 reviews

Java is one of the most popular programming languages that is used in software as well as for building different cross-platform applications. One of the biggest advantages of programs written in Java is that they are easily portable across different platforms/architectures.


This guide outlines the general steps to install the OpenJDK version of Java in different Linux distributions.


CentOS 7/8


  1. Run update before proceeding with the installation.

    # yum -y update
    

    java1

  2. Use the following command to install Java.

    # yum install java-1.8.0-openjdk
    

    java2

  3. To verify Java is installed, input the following command.

    # java -version
    

    java3


General Java configuration

While configuring Java, it is important that the correct installation of Java is set to be used by applications hosted in the server. This requires certain environment variables to be set in the server. The below steps can be made use of for this purpose.


  1. Execute the below command to list out the programs that provide java command in the server.

    # alternatives --config java
    

    A sample output will be as shown below:

    java4

    Select the number corresponding to the java executable that needs to be made use of as default.

  2. Another option to select which java executable is to be made use of, is to make use of environment variables such as JAVA_HOME. The value of this variable can be set to the path of the exact java executable to be used.

    For setting the environment variable, the below command can be executed (based on the above results).

    # export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-2.el8_1.x86_64/jre/bin/java
    

    java5

    Execute the below command to confirm that the Java path has been set correctly.

    # echo $JAVA_HOME
    

    java6


Testing Java

The installed Java service can be tested from the command line itself by following the below steps.


  1. Create a .java file with the help of any text editor.

    # vi test.java
    

    java7

  2. Type in the following code in the created file.

    class test {
    public static void main(String args[]){
    System.out.println("TEST JAVA SERVER!");
    }
    }
    

    java8

  3. Execute the following command to compile the java file.

    # javac test.java
    

    java9

  4. Use the following command to run the complied java file

    # java test
    

    java10


Debian/Ubuntu


  1. Run update before proceeding with the installation.

    # apt-get update && apt-get upgrade
    

    java11

  2. Install the repository.

    # add-apt-repository ppa:webupd8team/java
    

    java12

  3. Then run update once again using the following command.

    # apt-get update
    

    java13

  4. Use the following command to install Java.

    # apt install openjdk-11-jdk
    

    java14

  5. To verify Java has been installed, input the following command.

    # java -version
    

    java15


General Java configuration

While configuring Java, it is important that the correct installation of Java is set to be used by applications hosted in the server. This requires certain environment variables to be set in the server. The below steps can be made use of for this purpose.


  1. Execute the below command to list out the programs that provide java command in the server.

    # update-alternatives --config java
    

    A sample output will be as shown below:

    java16

  2. Another option to select which java executable is to be made use of, is to make use of environment variables such as JAVA_HOME. The value of this variable can be set to the path of the exact java executable to be used.

    For setting the environment variable, the below command can be executed (based on the above results).

    # export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java
    

    java17

    Execute the below command to confirm that the Java path has been set correctly.

    # echo $JAVA_HOME
    

    java18


Testing Java

The installed Java service can be tested from the command line itself by following the below steps.


  1. Create a .java file with the help of any text editor.

    # vi test.java
    

    java19

  2. Type in the following code in the created file.

    class test {
    public static void main(String args[]){
    System.out.println("TEST JAVA SERVER!");
    }
    }
    

    java20

  3. Execute the following command to compile the java file.

    # javac test.java
    

    java21

  4. Use the following command to run the complied java file.

    # java test
    

    java22

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.