To install a Java runtime in Ubuntu, you typically install either OpenJDK (the open-source default) or Oracle Java, using the apt package manager or a downloaded package. This guide covers both, focusing on the most common methods for current Ubuntu LTS releases.
What is a Java Runtime and Why Do You Need It?
Java programs are compiled into bytecode, which is not directly executable by your operating system. A Java runtime environment (JRE) provides the Java Virtual Machine (JVM) and core libraries needed to run that bytecode. When a program or website asks for Java, it usually means it needs a JRE. The JDK (Java Development Kit) includes the JRE plus compilers and tools for developing Java applications. For running a Java application, the JRE is sufficient, but many packages in Ubuntu depend on the full JDK.
Check if Java is Already Installed
Before installing anything, open a terminal and run:
java -version
If Java is installed, you’ll see version information like openjdk version "17.0.10". If not, you’ll get an error like command not found. Also check if a JDK is present with javac -version. Some systems have Java installed but not the compiler.
Choosing Between OpenJDK and Oracle Java
OpenJDK is the default and recommended choice for most Ubuntu users. It’s free, open-source, and included in Ubuntu’s repositories. Oracle Java is the official commercial build, which may have slightly different performance characteristics and licensing. For general use, OpenJDK is fully compatible and easier to install. If you specifically need Oracle Java (e.g., for a legacy application), you can download it from Oracle’s website, but you’ll need to accept their license agreement.
Method 1: Install OpenJDK via apt (Recommended)
This is the simplest and most reliable method. Ubuntu’s repositories contain several versions of OpenJDK. As of 2025, OpenJDK 11, 17, and 21 are commonly available.
- Update your package lists:
sudo apt update - Install the default JRE (which is OpenJDK):
sudo apt install default-jre - If you need the JDK (for compiling Java code):
sudo apt install default-jdk - Verify the installation:
java -version
To install a specific version, like OpenJDK 17 JRE:
sudo apt install openjdk-17-jre
For the JDK version of that release:
sudo apt install openjdk-17-jdk
Method 2: Install Oracle Java (Manual Download)
If you need Oracle’s build, download the .tar.gz from Oracle’s official downloads page (requires accepting their license). Then extract and install it manually:
- Download the Linux x64 archive (e.g.,
jdk-21_linux-x64_bin.tar.gz). - Create a directory for Java:
sudo mkdir -p /usr/local/java - Extract the archive there:
sudo tar -xzvf jdk-21_linux-x64_bin.tar.gz -C /usr/local/java - Update alternatives so the system knows where the new Java is:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk-21/bin/java" 1
Do the same forjavacif you installed the JDK. - Set it as the default:
sudo update-alternatives --config java
Select the number corresponding to your new installation.
Setting JAVA_HOME (Environment Variable)
Many Java applications require the JAVA_HOME environment variable to point to the Java installation directory. To set it globally, edit /etc/environment:
sudo nano /etc/environment
Add a line (adjust the path to match your actual Java directory):
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
For Oracle Java, it would be something like /usr/local/java/jdk-21. After saving, reload the file:
source /etc/environment
Verify with echo $JAVA_HOME.
Common Problems and Their Fixes
- “Unable to locate package”: Run
sudo apt updatefirst. If it persists, your Ubuntu version may not include that Java version in its repositories. - Multiple Java versions installed: Use
sudo update-alternatives --config javato switch the default. - Java not recognized after manual install: Ensure the
update-alternativescommand was run with the correct path, and that the path exists. - Permission denied when running a .jar file: Make sure the file is executable (
chmod +x file.jar) or run it withjava -jar file.jar.
Frequently Asked Questions
What is the difference between JRE and JDK?
The JRE (Java Runtime Environment) is what you need to run Java programs. The JDK (Java Development Kit) includes the JRE plus tools for developing, debugging, and compiling Java code. If you only need to run a program, install the JRE; if you plan to write Java code, install the JDK.
How do I uninstall Java?
To remove OpenJDK, use sudo apt remove default-jre or sudo apt remove openjdk-17-jre (adjust the version). For Oracle Java, you may need to manually delete the directory and remove the alternatives entries.
Is it safe to install Java from the Ubuntu repositories?
Yes, packages from the official Ubuntu repositories are maintained and security-patched by the Ubuntu community. It’s the safest way to install Java on Ubuntu.
Can I have multiple Java versions installed at once?
Yes, you can install multiple versions. Use update-alternatives to choose which one is the default for the java command. You can also set JAVA_HOME per application or per user.
Why does a Java program say “Java not found” even after installation?
This usually means the java executable is not in your PATH. Check with which java. If it’s empty, you may have installed only the JDK but not the JRE, or the installation didn’t add the symlink correctly. Reinstall via apt or manually add the bin directory to your PATH.
Does Ubuntu include Java by default?
No, Ubuntu does not come with Java pre-installed. You need to install it manually using the methods above.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Add & Remove Favorites in Windows 10 Photos App
- Fix Links Not Opening in Chrome: 7 Top Ways
- Connect Alexa to a Soundbar: Choose the Right Setup
- Fix Host Process for Setting Synchronization in Windows 10
- Windows 11 search how to control automatic indexing of frequently used folders?
- How to Set Up Tips on Twitter (X) for Android & iOS