How to Install Java on Ubuntu: A Step-by-Step Guide

How to Install Java on Ubuntu: A Step-by-Step Guide

Java is a widely used programming language, essential for developing applications, running enterprise solutions, and supporting many popular software tools. If you're using Ubuntu and need to install Java, this guide will walk you through the step-by-step process.

Step 1: Update Your Package List

Before installing Java, update your system's package list to ensure you're getting the latest versions:

sudo apt update && sudo apt upgrade -y        

Step 2: Check if Java is Already Installed

To see if Java is already installed on your system, run:

java -version        

If Java is installed, this command will return the installed version. If not, you’ll see an error message.

Step 3: Install OpenJDK (Recommended)

Ubuntu offers OpenJDK, an open-source version of Java. To install the latest version, run:

sudo apt install default-jdk -y        

To install a specific version, such as Java 17, use:

sudo apt install openjdk-17-jdk -y        

Step 4: Verify the Installation

Once installed, check the Java version again:

java -version        

You should see output confirming the installed version of Java.

Step 5: Set the Default Java Version (Optional)

If multiple versions of Java are installed, you can set the default version using:

sudo update-alternatives --config java        

Select the desired version from the list.

Step 6: Install Oracle JDK (Alternative Method)

For those who prefer Oracle JDK, follow these steps:

  1. Download the latest Oracle JDK from Oracle’s website.
  2. Extract the downloaded file:
  3. Move the extracted files to /usr/lib/jvm/:
  4. Set the environment variables:
  5. Verify the installation:

Step 7: Set Up JAVA_HOME Environment Variable (Optional)

To configure the JAVA_HOME variable, edit the ~/.bashrc file:

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

Verify by running:

echo $JAVA_HOME        

By following these steps, you can successfully install Java on Ubuntu and set up your development environment. Whether you choose OpenJDK or Oracle JDK, ensuring your Java installation is up-to-date is crucial for running and developing Java applications.

Have you installed Java on Ubuntu before? Share your experience and tips in the comments!

?? Watch the full installation video on my YouTube channel: AAA Tech

要查看或添加评论,请登录

Abdullah bin Amin的更多文章

社区洞察

其他会员也浏览了