On this page
- Which Java version should you install?
- How do you install OpenJDK from the distribution?
- How do you install a newer LTS from Adoptium?
- How do you check the install and switch versions?
- How do you set JAVA_HOME?
- How do you add Maven and Gradle?
- How do you run a Java app as a service?
- Which limits matter most on a small plan?
- Questions
Which Java version should you install?
Install the LTS release your application documents, and prefer the newest LTS for new projects. OpenJDK publishes long-term-support releases every two years: 17, 21 and 25 are the current ones, while short-lived feature releases in between are best left to development machines. Application servers, build tools and frameworks state their minimum, for example Spring Boot 3 needs 17 or newer.
What the distributions carry differs, so check before choosing a method:
# Ubuntu and Debian
apt-cache search --names-only '^openjdk-[0-9]+-jdk-headless$'
# Rocky Linux and AlmaLinux 9
dnf list available 'java-*-openjdk-devel'As a rough guide, Ubuntu 24.04 and 22.04 offer 17 and 21, Debian 12 offers 17, Debian 13 offers 21, and Rocky and Alma 9 offer 11, 17 and 21, with newer majors added in later point releases. The output of the commands above is the authority for your exact image.
How do you install OpenJDK from the distribution?
Use the headless packages on a server; they leave out the desktop libraries for fonts and windows that a service never needs. The JDK includes the runtime, so one package covers both compiling and running.
# Ubuntu and Debian
sudo apt update
sudo apt install -y openjdk-21-jdk-headless
# Rocky Linux and AlmaLinux 9
sudo dnf install -y java-21-openjdk-develIf you only run prebuilt jars, the runtime alone is smaller: openjdk-21-jre-headless or java-21-openjdk-headless. Security updates arrive through normal apt upgrade or dnf upgrade runs, which is the main reason to prefer packaged builds. Turn those on with automatic security updates.
How do you install a newer LTS from Adoptium?
When your distribution lacks the release you need, such as Java 25 on Debian 12, the Eclipse Adoptium project publishes Temurin builds in signed apt and dnf repositories. Temurin is a standard OpenJDK build that passes the compatibility test suite.
# Ubuntu and Debian
sudo apt install -y wget gpg
wget -qO- https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor | sudo tee /etc/apt/keyrings/adoptium.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb $(. /etc/os-release; echo $VERSION_CODENAME) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
sudo apt update
sudo apt install -y temurin-25-jdk# Rocky Linux and AlmaLinux 9
sudo tee /etc/yum.repos.d/adoptium.repo > /dev/null <<'EOF'
[Adoptium]
name=Adoptium
baseurl=https://packages.adoptium.net/artifactory/rpm/rhel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public
EOF
sudo dnf install -y temurin-25-jdkThe repository keeps every Temurin LTS line, so temurin-17-jdk and temurin-21-jdk are available the same way.
How do you check the install and switch versions?
Print the runtime and compiler versions, then run a one-file program. Since Java 11, java can launch a single source file without a separate compile step.
java -version
javac -version
cat > Hello.java <<'EOF'
public class Hello {
public static void main(String[] args) {
System.out.println("Java " + Runtime.version() + " works");
}
}
EOF
java Hello.javaWith several JDKs installed, the alternatives system decides which one java means. Choose interactively, or script it:
# Ubuntu and Debian
sudo update-alternatives --config java
sudo update-alternatives --config javac
# Rocky Linux and AlmaLinux 9
sudo alternatives --config java
sudo alternatives --config javacHow do you set JAVA_HOME?
Derive JAVA_HOME from the active compiler so it follows the alternatives choice. Many tools, including Maven and Gradle, read it.
echo 'export JAVA_HOME=$(dirname $(dirname $(readlink -f $(command -v javac))))' | sudo tee /etc/profile.d/java-home.sh
source /etc/profile.d/java-home.sh
echo $JAVA_HOMEThe result is something like /usr/lib/jvm/java-21-openjdk-amd64 on Debian systems or /usr/lib/jvm/java-21-openjdk-21.0.x... on the RHEL family. For a systemd service, set JAVA_HOME or the full java path in the unit instead, because units do not read profile scripts.
How do you add Maven and Gradle?
Maven is packaged everywhere and recent enough for most builds. Gradle in the distributions is either missing or very old, so use the Gradle Wrapper that most projects ship (./gradlew), which downloads the exact Gradle version the project expects.
# Maven
sudo apt install -y maven # Ubuntu and Debian
sudo dnf install -y maven # Rocky and Alma
mvn -version
# Gradle through a project wrapper
cd ~/apps/inventory
./gradlew --version
./gradlew buildIf you need a standalone Gradle, for example to create a wrapper in a new project, unpack the official distribution into /opt. Take the current version number from the Gradle releases page:
GRADLE_VER=9.2.0
sudo apt install -y unzip # or: sudo dnf install -y unzip
curl -fsSLO "https://services.gradle.org/distributions/gradle-${GRADLE_VER}-bin.zip"
sudo unzip -q -d /opt/gradle "gradle-${GRADLE_VER}-bin.zip"
sudo ln -sfn "/opt/gradle/gradle-${GRADLE_VER}" /opt/gradle/current
echo 'export PATH=$PATH:/opt/gradle/current/bin' | sudo tee /etc/profile.d/gradle.sh
source /etc/profile.d/gradle.sh
gradle --versionBuild tools cache dependencies under ~/.m2 and ~/.gradle; those folders grow over time, so check them with the disk usage guide if space runs low.
How do you run a Java app as a service?
Copy the built jar to a fixed location and write a unit that starts it as a dedicated user with a memory ceiling that fits the plan.
sudo useradd --system --home /opt/inventory --shell /usr/sbin/nologin inventory
sudo mkdir -p /opt/inventory
sudo cp build/libs/inventory.jar /opt/inventory/
sudo chown -R inventory: /opt/inventory
sudo nano /etc/systemd/system/inventory.service[Unit]
Description=Inventory Java service
After=network.target
[Service]
User=inventory
WorkingDirectory=/opt/inventory
ExecStart=/usr/bin/java -XX:MaxRAMPercentage=70 -jar /opt/inventory/inventory.jar --server.address=127.0.0.1 --server.port=8080
SuccessExitStatus=143
Restart=on-failure
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now inventory
journalctl -u inventory -fSuccessExitStatus=143 tells systemd that the JVM exiting on SIGTERM is a clean stop. The --server.* flags are Spring Boot options; other frameworks have their own. Keep the port on localhost and publish it through a reverse proxy as in the Nginx reverse proxy guide; if you must expose it for a test, use sudo ufw allow 8080/tcp or sudo firewall-cmd --add-port=8080/tcp and remove the rule afterwards.
Which limits matter most on a small plan?
Memory is the usual limit. The JVM sizes its heap from the RAM it sees, and percentage flags such as MaxRAMPercentage keep it from crowding out the OS. On a 1 GB plan, one small service is realistic; builds with Maven or Gradle plus a running app want 2 to 4 GB. If the kernel kills the process, journalctl -k | grep -i oom shows it; lower the heap share or move to a bigger plan with the migration tutorial. Gradle daemons stay resident after a build, so stop them with ./gradlew --stop on tight machines.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Install Java (OpenJDK), Maven and Gradle
What is the difference between JDK and JRE packages?
The JDK adds the compiler and tools such as javac and jcmd. Servers that only run finished jars can use the smaller runtime package, but the JDK helps with diagnostics.
Is Temurin the same as OpenJDK?
Temurin is a build of the OpenJDK source code by the Eclipse Adoptium project, tested for compatibility. Apps behave the same as on distribution OpenJDK builds.
Why does java -version show an older release after installing a new one?
The alternatives link still points at the old JDK. Run update-alternatives --config java or alternatives --config java and pick the new path.
Do I need Tomcat to run a Java web app?
Not for apps packaged as executable jars with an embedded server, which is the common case today. Classic WAR files need a servlet container, covered in the Tomcat guide.
How do I remove an old Java version?
Uninstall its package, for example sudo apt remove openjdk-17-jdk-headless or sudo dnf remove java-17-openjdk-devel, then confirm the alternatives point at a version you kept.