How to Install and Set Up Java 17 on Ubuntu

Java 17, as a long-term support (LTS) version, is widely used due to its new features, security updates, and performance improvements. This guide will walk you through installing Java 17 on an Ubuntu system, setting up environment variables, and verifying the installation.


Prerequisites

Before we begin, ensure that you have:

  • Ubuntu 18.04 or higher
  • sudo privileges on your user account
  • Internet access to download the required packages

Step 1: Update System Packages

Start by updating your package list to ensure you have the latest repositories:

sudo apt update && sudo apt upgrade -y

Step 2: Install Java 17

Ubuntu's official repositories include OpenJDK, an open-source version of Java. You can install OpenJDK 17 directly using apt:

sudo apt install openjdk-17-jdk -y

This command installs the Java Development Kit (JDK) version 17, which includes the Java Runtime Environment (JRE) and tools necessary for Java development.

Step 3: Verify the Installation

After installation, confirm that Java 17 is installed and configured correctly:

java -version

You should see output similar to this:

openjdk version "17.0.x" <build number>
OpenJDK Runtime Environment (build 17.0.x)
OpenJDK 64-Bit Server VM (build 17.0.x)

This confirms that Java 17 is installed and running.

Step 4: Set JAVA_HOME Environment Variable

Setting the JAVA_HOME environment variable is essential for many Java applications, as it points to the Java installation directory. Here’s how to set it up:

Locate Java 17 Installation Path:

Use the following command to find the installation path of Java 17:

sudo update-alternatives --config java

You’ll see a list of installed Java versions. The path to Java 17 will look something like /usr/lib/jvm/java-17-openjdk-amd64.

Set JAVA_HOME in Profile:

Open your profile configuration file using a text editor:

sudo nano /etc/profile

Add JAVA_HOME to Profile:

Scroll to the end of the file and add the following lines to set JAVA_HOME:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Apply the Changes:

Save and close the file (in nano, press Ctrl + O, then Enter, then Ctrl + X). Reload the profile file to apply the changes:

source /etc/profile

Verify JAVA_HOME:

Check if the JAVA_HOME variable is set correctly by running:

echo $JAVA_HOME

This should display the path you set, such as /usr/lib/jvm/java-17-openjdk-amd64.

Step 5: Setting Default Java Version (Optional)

If you have multiple versions of Java installed, you can configure Ubuntu to use Java 17 as the default by using the update-alternatives tool.

Configure Java Versions:

sudo update-alternatives --config java

You’ll see a list of installed Java versions. Enter the number corresponding to the Java 17 installation path.

Verify Default Java Version:

Run the following command to ensure Java 17 is set as the default:

java -version

Step 6: Test the Java Installation

Finally, let’s create a simple Java program to verify the setup:

Create a Java File:

Create a file called HelloJava.java:

nano HelloJava.java

Add Java Code:

Paste the following Java code into the file:

public class HelloJava {
    public static void main(String[] args) {
        System.out.println("Java 17 is installed and running! Thanks to InsightForGeeks");
    }
}

Save and close the file.

Compile and Run the Program:

Compile and run the Java file:

javac HelloJava.java
java HelloJava
  1. If everything is set up correctly, you should see:
Java 17 is installed and running! Thanks to InsightForGeeks

Uninstalling Java 17 (Optional)

If you need to remove Java 17 for any reason, you can uninstall it with the following command:

sudo apt remove openjdk-17-jdk -y

Wrapping Up

You now have Java 17 installed and configured on your Ubuntu system. With Java 17 LTS, you can take advantage of new language features, better performance, and long-term support for your applications.

Related Posts

Recent Posts

Suzlon Energy Q4 Results: Key Takeaways

Understanding the Risks of Cookies Without the Secure Flag Set

How to prevent BEAST Vulnerability

How to Solve LUCKY13 Vulnerability (Potentially Vulnerable)

സ്കൂൾ പ്രവേശനോത്സവത്തിന്‍റെ സംസ്ഥാനതല ഉദ്ഘാടനം ജൂൺ മൂന്നിന് കൊച്ചിയിൽ

പകർച്ച വ്യാധി ഭീഷണിയിൽ ഇടുക്കി, ഡെങ്കിപ്പനി കേസുകളിൽ വൻവർധന, പ്രതിരോധ പ്രവർത്തനങ്ങൾ ഊർജിതമാക്കി ആരോഗ്യവകുപ്പ്

ഇടുക്കിയിലെ മലയോര മേഖലകളിൽ രാത്രിയാത്ര നിരോധിച്ചു. രാത്രി ഏഴു മുതൽ രാവിലെ ആറു വരെയാണ് നിരോധനം

ഏന്തയാർ ഈസ്റ്റിൽ പ്രളയത്തിൽ തകർന്ന പാലത്തിന് പകരം പുതിയ പാലം നിർമ്മിക്കുവാൻ താത്ക്കാലിക പാലം പൊളിച്ച് നീക്കി

Explore the Investment Opportunities: A Comprehensive Guide to Different Types of Mutual Funds

Title: Understanding Mutual Funds: A Beginner's Guide to Investing