Java is a powerful programming language and a crucial component for many applications and development environments. Whether you’re working on web applications, Android development, or backend systems, Java is a necessary tool. In this guide, we’ll show you how to install Java on Ubuntu, covering both the OpenJDK and Oracle JDK options.
Method 1: Installing OpenJDK
OpenJDK is an open-source implementation of the Java Platform and is readily available through Ubuntu’s default repositories.
Step 1: Open Terminal
Press Ctrl + Alt + T
to open your terminal.
Step 2: Update the Package Repository
Before installing anything, make sure your package list is up to date:
sudo apt update
Step 3: Install OpenJDK
To install the default JDK, run the following command:
sudo apt install default-jdk
This command installs both the Java Development Kit (JDK) and Java Runtime Environment (JRE).
Step 4: Verify Installation
After the installation is complete, verify it by checking the Java version:
java -version
You should see output indicating the installed Java version.
Method 2: Installing Oracle JDK
Oracle’s JDK offers additional features not included in OpenJDK, and you can install it from a third-party repository.
Step 1: Install Required Dependencies
First, ensure that you have the software-properties-common
package installed:
sudo apt install software-properties-common
Step 2: Add the Oracle PPA
Next, add the repository that contains Oracle’s JDK installer:
sudo add-apt-repository ppa:linuxuprising/java -y
Step 3: Update the Package Repository
Update your package list again to include the new repository:
sudo apt update
Step 4: Install Oracle JDK
To install Oracle JDK 17, run the following command:
sudo apt install oracle-java17-installer
You’ll be prompted to accept the Oracle license agreement during installation.
Step 5: Set Oracle JDK as Default (Optional)
If you have multiple Java versions installed and want Oracle JDK to be the default, run:
sudo update-alternatives --config java
Follow the prompts to select your preferred Java version.
Step 6: Verify Installation
Finally, confirm the installation by checking the version:
java -version
Optional: Setting the JAVA_HOME Environment Variable
Some applications require the JAVA_HOME
environment variable to be set. Here’s how to set it up.
Step 1: Find the Java Installation Path
Run the following command to find where Java is installed:
readlink -f $(which java)
Step 2: Edit the Environment File
Open the /etc/environment
file in a text editor:
sudo nano /etc/environment
Step 3: Add the JAVA_HOME Variable
Add the following line at the end of the file, replacing /path/to/java
with the actual Java installation path (e.g., /usr/lib/jvm/java-17-oracle
):
JAVA_HOME="/path/to/java"
Step 4: Apply Changes
To apply the changes, run:
source /etc/environment
Conclusion
Whether you choose OpenJDK or Oracle JDK, installing Java on Ubuntu is a straightforward process. By following this guide, you can set up Java for your development needs in no time. For more tutorials and tips, visit CodeAllow.