Install Java on Ubuntu

Install Java on Ubuntu

Learn how to install Java on Ubuntu in a few simple steps.

Alex Rodriguez
2024-01-10
2 min read
Table of Contents

To install Java and run Java code in Linux (Ubuntu or other distributions), follow these steps:

Step 1: Install Java

Option 1: Install OpenJDK (Recommended)

Check Available Java Versions

SH
sudo apt update
apt search openjdk

Install OpenJDK 17 (Recommended)

SH
sudo apt install -y openjdk-17-jdk

To install a different version (e.g., Java 21), replace openjdk-17-jdk with openjdk-21-jdk.

Verify Java Installation

SH
java -version
javac -version

Option 2: Install Oracle JDK (If Required)

Oracle JDK requires manual installation. Use this if OpenJDK is not suitable for your needs.

  1. Download Oracle JDK from the official Oracle website.
  2. Extract it and set up environment variables manually.

Step 2: Run Java Code

Method 1: Compile and Run Java Code (Without an IDE)

  1. Create a Java file:
    SH
    nano HelloWorld.java
    
  2. Add the following Java code:
    JAVA
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
    
  3. Save the file (Ctrl + X, then Y, then Enter).
  4. Compile the Java file:
    SH
    javac HelloWorld.java
    
    This generates a HelloWorld.class file.
  5. Run the Java program:
    SH
    java HelloWorld
    
    Expected output:
    TEXT
    Hello, World!
    

Method 2: Run Java Code Using jshell (Interactive Mode)

If you installed Java 9+, you can use jshell for quick testing:

SH
jshell

Then type:

JAVA
System.out.println("Hello, World!");

Press Enter, and it will print:

TEXT
Hello, World!

Exit jshell using:

SH
/exit

Step 3: Set JAVA_HOME (If Needed)

To set the JAVA_HOME environment variable:

SH
echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))" >> ~/.bashrc
echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc

Check if it’s set:

SH
echo $JAVA_HOME
Share this article:
42 likes
Alex Rodriguez

Alex Rodriguez

JavaScript Instructor

JavaScript expert and former bootcamp instructor. Specializes in making complex concepts simple and engaging for new developers.

Related Articles

Install Docker on Ubuntu
#Docker
#Ubuntu

Install Docker on Ubuntu

Learn how to install Docker on Ubuntu in a few simple steps.

Read More
Install Node.js Using NVM
#Node.js
#NVM

Install Node.js Using NVM

Learn how to install Node.js using NVM (Node Version Manager) on Linux in a few simple steps.

Read More
Comprehensive Guide to Package Managers in Software Development
#Package Managers
#NPM

Comprehensive Guide to Package Managers in Software Development

A detailed comparison of package managers across different programming languages to help you choose the best one for your development journey.

Read More
Mastering Async Programming in JavaScript
#JavaScript
#async

Mastering Async Programming in JavaScript

Learn how to handle asynchronous operations in JavaScript with promises, async/await, and modern patterns.

Read More
Fix Line Ending Issues in VS Code
#VS Code
#Line Endings

Fix Line Ending Issues in VS Code

Learn how to fix CRLF (Carriage Return + Line Feed) and LF (Line Feed) issues in VS Code for consistent line endings in your files.

Read More

Never Miss an Update

Get the latest tutorials, tips, and insights delivered straight to your inbox.

No spam, unsubscribe at any time.