Installing Java on Linux using SSH


Java is a popular programming language that is widely used for developing various types of software applications. Linux is one of most popular operating systems used for software development due to its stability, security, and open-source nature. In this article, we will discuss how to install Java on Linux using SSH.

SSH (Secure Shell) is a secure network protocol used for remote login to a server. It allows users to log in to a remote server and perform various operations using command-line tools. This makes it an excellent choice for installing Java on a Linux machine. We will be using OpenJDK package for this installation, which is a free and open-source implementation of Java Development Kit.

In this article, we will cover following topics

  • Checking for Java Installation

  • Installing Java on Linux

  • Setting Java Environment Variables

  • Testing Java Installation

Checking for Java Installation

Before we start installation process, we need to check if Java is already installed on Linux machine. To do this, we will use command-line tool, which can be accessed using SSH protocol.

  • Step 1 − Open your terminal or command-line tool and log in to your remote server using SSH.

  • Step 2 − Enter following command to check if Java is installed on your system −

java -version

If Java is already installed on your system, this command will display version of Java installed. If Java is not installed on your system, you will get an error message saying "command not found."

Installing Java on Linux

Once we have confirmed that Java is not installed on our system, we can proceed with installation process. We will be using OpenJDK package, which is available in default package repositories for most Linux distributions.

  • Step 1 − Log in to your remote server using SSH.

  • Step 2 − Enter following command to update package repository −

sudo apt update

This command will update package repository and download latest package information.

  • Step 3 − Enter following command to install OpenJDK package −

sudo apt install default-jdk

This command will download and install OpenJDK package on your Linux system. You may be prompted to enter your password to authenticate installation process.

Setting Java Environment Variables

After installing Java on your Linux machine, we need to set environment variables to point to Java installation directory. This will allow other applications to access Java installation.

  • Step 1 − Enter following command to open environment variables file −

sudo nano /etc/environment

This command will open environment variables file in nano text editor.

  • Step 2 − Add following lines to file to set Java environment variables −

JAVA_HOME=/usr/lib/jvm/default-java
PATH="$PATH:$JAVA_HOME/bin"
  • Step 3 − Save changes and exit nano text editor.

  • Step 4 − Enter following command to apply changes −

source /etc/environment

This command will apply changes to environment variables.

Testing Java Installation

After installing Java and setting environment variables, we need to test if installation was successful. We will use a simple Java program to test installation.

  • Step 1 − Enter following command to open a new file −

nano TestJava.java

This command will open a new file in nano text editor.

  • Step 2 − Add following lines to file −

public class TestJava {
   public static void main(String[] args) {
      System.out.println("Java installed successfully");
   }
}

This is a simple Java program that prints a message on screen.

  • Step 3 − Save file and exit nano text editor.

  • Step 4 − Enter following command to compile Java program −

javac TestJava.java

This command will compile Java program and generate a class file.

  • Step 5 − Enter following command to run Java program −

java TestJava

This command will run Java program and display message "Java installed successfully" on screen. If you see this message, it means that Java has been successfully installed on your Linux machine.

In addition to OpenJDK package, there are several other Java Development Kit (JDK) packages available for Linux, including Oracle JDK, Amazon Corretto, and Zulu. installation process for these packages is similar to one outlined in this article, but package names and repository locations may be different.

It's important to note that OpenJDK package is recommended choice for most users as it is free, open-source, and maintained by a community of developers. Oracle JDK, on other hand, is a commercial product that comes with additional features and support but requires a license for commercial use.

In some cases, you may encounter issues while installing Java on Linux, such as dependency conflicts or package installation errors. In such cases, it's recommended to check documentation of your Linux distribution or Java package for troubleshooting tips and solutions.

Another important consideration is version of Java you choose to install. Java is backward compatible, which means that applications developed on an older version of Java will still run on a newer version. However, it's not forward compatible, which means that applications developed on a newer version of Java may not run on an older version.

Therefore, it's recommended to install latest version of Java that is compatible with your application requirements. You can check compatibility of your application with different Java versions by referring to documentation or contacting application developer.

In addition to installing Java on Linux using SSH, you can also use package managers such as apt or yum to install Java. These package managers automate installation process and handle dependencies, making it easier and faster to install Java and keep it up to date.

Conclusion

Installing Java on Linux using SSH is a simple process that can be completed in a few easy steps. By following steps outlined in this article, you can install Java on your Linux machine and set up environment variables to allow other applications to access Java installation. With Java installed, you can start developing Java applications on your Linux machine and take advantage of many benefits of this powerful programming language.

Remember to always use latest version of Java, which can be downloaded from official Java website. Also, make sure to keep your Linux system up to date with latest security patches to ensure security and stability of your system. With these precautions in mind, you can enjoy a smooth and hassle-free experience while developing your Java applications on Linux.

Updated on: 14-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements