Installing Java on Linux using SSH

Java is a popular programming language widely used for developing various types of software applications. Linux is one of the most popular operating systems 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 the OpenJDK package for this installation, which is a free and open-source implementation of the Java Development Kit.

Prerequisites

  • SSH access to a Linux server

  • User account with sudo privileges

  • Internet connection for downloading packages

Checking for Java Installation

Before starting the installation process, we need to check if Java is already installed on the Linux machine. To do this, we will use command-line tools accessible through SSH.

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

Step 2 Enter the 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 the version of Java installed. If Java is not installed, 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 the installation process. We will be using the OpenJDK package, which is available in the default package repositories for most Linux distributions.

For Ubuntu/Debian Systems

Step 1 Update the package repository

sudo apt update

Step 2 Install the default OpenJDK package

sudo apt install default-jdk

For CentOS/RHEL/Fedora Systems

Step 1 Update the package repository

sudo yum update
# or for newer versions
sudo dnf update

Step 2 Install OpenJDK

sudo yum install java-11-openjdk-devel
# or for newer versions
sudo dnf install java-11-openjdk-devel

Setting Java Environment Variables

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

Step 1 Find the Java installation path

sudo update-alternatives --config java

Step 2 Open the environment variables file

sudo nano /etc/environment

Step 3 Add the following lines to set Java environment variables

JAVA_HOME=/usr/lib/jvm/default-java
PATH="$PATH:$JAVA_HOME/bin"

Step 4 Save changes and exit nano text editor (Ctrl+X, then Y, then Enter).

Step 5 Apply the changes to environment variables

source /etc/environment

Testing Java Installation

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

Step 1 Create a new Java file

nano TestJava.java

Step 2 Add the following Java code

public class TestJava {
    public static void main(String[] args) {
        System.out.println("Java installed successfully!");
        System.out.println("Java version: " + System.getProperty("java.version"));
    }
}

Step 3 Save the file and exit nano text editor.

Step 4 Compile the Java program

javac TestJava.java

Step 5 Run the Java program

java TestJava

Expected output

Java installed successfully!
Java version: 11.0.x

Java Version Comparison

Java Distribution License Support Best For
OpenJDK Open Source Community Development & Production
Oracle JDK Commercial Oracle Enterprise Applications
Amazon Corretto Open Source Amazon AWS Applications
Azul Zulu Open Source Azul Cross-platform Applications

Common Issues and Solutions

  • Permission denied Ensure you have sudo privileges and use sudo for installation commands.

  • Package not found Update your package repository first using apt update or yum update.

  • Environment variables not working Restart your terminal session or run source ~/.bashrc.

  • Multiple Java versions Use sudo update-alternatives --config java to switch between versions.

Conclusion

Installing Java on Linux using SSH is a straightforward process that involves checking for existing installations, installing the OpenJDK package, setting environment variables, and testing the installation. OpenJDK is the recommended choice for most users as it is free, open-source, and well-maintained. With Java properly installed and configured, you can begin developing Java applications on your Linux system efficiently.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements