Support Online
Skip to main content

Installing Java on Ubuntu Server (JRE and JDK)

The Java Virtual Machine (JVM) is required for many applications such as Jenkins, Apache Tomcat or Glassfish.
In this guide, you will learn how to install Java Runtime Environment (JRE) and Java Development Kit (JDK) on Ubuntu.
You'll also see the differences between OpenJDK and Oracle JDK versions, version management, and the JAVA_HOME setting.

🧠 Technical Summary

CriterionDescription
Main TopicInstalling Java (JRE & JDK) using apt in Ubuntu
Solved ProblemCreating the necessary environment to run or develop Java applications
ConclusionYour Ubuntu server becomes ready for Java software.

⚙️ Requirements

To follow this guide:

  • A GenixNode virtual server (instance) with Ubuntu operating system
  • A user account with sudo authority is required.

☕ 1. Installing OpenJDK in Ubuntu (Easy Method)

OpenJDK is the open source version of Java supported by Ubuntu by default.
It usually comes with Java 11 or 17 packages installed.

🔹 1.1 Update Packages

sudo apt update

Make sure the package manager is up to date.

🔹 1.2 JRE Installation (Running Environment)


sudo apt install default-jre

This command installs the OpenJDK JRE — sufficient to run Java applications.

Check the installation:


java -version

🔹 1.3 JDK Installation (For Developers)


sudo apt install default-jdk

JDK includes compiler (javac) and debugging tools.

Verification:


javac -version

🏗️ 2. Oracle JDK 11 Installation (Manual Method)

Oracle JDK is not installed directly with apt due to licensing restrictions. Therefore manual download and a third-party PPA repository are required.

⚠️ Note: Use OpenJDK if possible. Carefully review the Oracle JDK license terms.

🔹 Download 2.1 Oracle JDK

Download the file in .tar.gz format from the Oracle Archive Downloads page.


scp Downloads/jdk-11.0.13_linux-x64_bin.tar.gz kullanici@sunucu_ip:~

This script transfers the file from your local machine to the server.

🔹 2.2 Add PPA and Keys


sudo gpg --no-default-keyring --keyring /usr/share/keyrings/oracle-jdk11-installer.gpg --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

Oracle adds the required key for the installation signature.


echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-jdk11-installer.gpg] https://ppa.launchpadcontent.net/linuxuprising/java/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/oracle-jdk11.list
sudo apt update

🔹 2.3 Move and Install Archive


sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
sudo cp jdk-11.0.13_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
sudo apt install oracle-java11-installer-local

Once the Oracle JDK installation is complete, you will be asked to accept the license agreement.


🔄 3. Managing Java Versions

If you have more than one Java version, you can switch with the update-alternatives command.


sudo update-alternatives --config java

Sample output:


0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode

Select the version number you want. You can also use the same command for tools such as javac, keytool, javadoc.


🌍 4. Defining the JAVA_HOME Environment Variable

JAVA_HOME is required for the operation of many Java-based applications (e.g. Jenkins, Tomcat).

🔹 4.1 Java Path Detection


sudo update-alternatives --config java

Example path:


/usr/lib/jvm/java-11-openjdk-amd64/bin/java

🔹 4.2 Adding Environment Variable


sudo nano /etc/environment

Add the following line:


JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

🔹 4.3 Activating Changes


source /etc/environment
echo $JAVA_HOME

If the correct path appears, the setting is successful.


🚨 Common Mistakes and Solutions

ErrorReasonSolution
Java not foundJava is not installed or added to PATHsudo apt install default-jdk
JAVA_HOME is not setEnvironment variable not definedAdd to file /etc/environment
Permission deniedLack of authorityRun commands with sudo
Cannot locate JDK/JREWrong directoryVerify with echo $JAVA_HOME

💬 Frequently Asked Questions (FAQ)

  1. What is the difference between JRE and JDK?
FeatureJREJDK
Running Environment
Compiler / Tools
Area of ​​UseRunning softwareSoftware development
  1. How do I switch between Java versions?

sudo update-alternatives --config java
  1. Why is JAVA_HOME important?

Many applications read the Java directory from this variable. If set incorrectly, programs cannot be started.

  1. What if I want to uninstall Java?

sudo apt remove openjdk-11-jdk

🏁 Conclusion

You have now installed both OpenJDK and Oracle JDK on your Ubuntu system. You also learned how to switch between multiple versions and correctly define the JAVA_HOME variable.

☁️ By applying all these steps on your GenixNode servers, you can run your Java-based applications in a safe and optimized environment.

markdown