⬅ Previous Topic
Introduction to JavaNext Topic ⮕
Install Java on Windows 10⬅ Previous Topic
Introduction to JavaNext Topic ⮕
Install Java on Windows 10In this guide, we’ll walk you through how to install Java on any major Linux distribution.
Before you install anything, it's good practice to check whether Java is already on your system.
java -version
Command 'java' not found, but can be installed with:
sudo apt install default-jre
If you see something like the above message, Java isn’t currently installed. If Java is installed, you'll see an output like this:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-222.04, mixed mode, sharing)
OpenJDK is the open-source implementation of Java. It is stable, secure, and available directly through most Linux package managers.
Open a terminal and run the following commands based on your Linux distribution.
sudo apt update
sudo apt install default-jdk
sudo dnf install java-17-openjdk-devel
sudo pacman -S jdk-openjdk
After installation, confirm the Java version:
java -version
openjdk version "17.0.8" 2023-07-18
If your project specifically requires Oracle JDK, you can download and install it manually. Oracle JDK includes some commercial features and licensing considerations.
Go to the official Oracle JDK downloads page and download the appropriate version for Linux. Then run:
cd ~/Downloads
tar -xvf jdk-21_linux-x64_bin.tar.gz
sudo mv jdk-21 /usr/lib/jvm/jdk-21
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 1
sudo update-alternatives --config java
It's important to let your system know where Java is located. To do this, follow these steps:
Use any text editor to open your shell configuration file. Use the command based on your shell:
# For bash users
nano ~/.bashrc
# For zsh users
nano ~/.zshrc
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
In nano, press Ctrl + O
to save, then Ctrl + X
to exit.
Run the following command to apply the changes immediately:
# For bash
source ~/.bashrc
# For zsh
source ~/.zshrc
Let’s test our installation with a simple Hello World program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java on Linux!");
}
}
javac HelloWorld.java
java HelloWorld
Hello, Java on Linux!
If you see this output, your Java installation is working perfectly!
For most developers, OpenJDK is sufficient and easily manageable through package managers. However, if you’re working in an enterprise setting where Oracle JDK is mandatory, manual installation offers full control.
Now that Java is ready to go, you can begin building Java applications.
⬅ Previous Topic
Introduction to JavaNext Topic ⮕
Install Java on Windows 10You can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.