HOW IT WORKS – Install and Configure Java 17 on Ubuntu
Overview
This guide explains how to check the current Ubuntu and Java configuration, install the OpenJDK 17 Development Kit, identify the installation directory, configure JAVA_HOME, and verify the Java runtime and compiler.
What you will complete
- Check the Ubuntu version and current Java configuration.
- Install OpenJDK 17 JDK using APT.
- Identify the Java 17 installation directory.
- Set JAVA_HOME in /etc/environment.
- Verify java, javac, and the executable referenced by JAVA_HOME.
Before you begin
- Ubuntu 24.04 LTS or Ubuntu 22.04 LTS is installed and you can open a terminal.
- Your account has sudo privileges.
- An internet connection is available to download packages.
- The example path is for a typical 64-bit Ubuntu installation; confirm your actual JDK path before using it.
Check the Current System Configuration
Before installing Java, inspect the operating system and any existing Java configuration.
Check the Ubuntu version
Displays Ubuntu release and codename information.
lsb_release -a
Check the Java runtime
Shows the version launched by the java command.
java -version
Check the Java compiler
Shows the compiler version. The compiler is provided by the JDK.
javac -version
Check JAVA_HOME
Displays the JAVA_HOME value for the current shell; an empty result means it is not set here.
echo "$JAVA_HOME"
Locate the active Java executable
Resolves the java command to its target path.
readlink -f "$(command -v java)"
Install OpenJDK 17
Refresh the APT package index:
sudo apt update
Install the OpenJDK 17 Development Kit:
sudo apt install openjdk-17-jdk -y
The JDK includes the Java runtime and development tools such as javac. These tools are needed to compile Java programs and are commonly required by Java build systems.
Verify the Java 17 Installation Path
List the Java 17 directories under /usr/lib/jvm:
ls -d /usr/lib/jvm/java-17-openjdk-*
Resolve the active Java executable:
readlink -f "$(command -v java)"
A typical Java home path on 64-bit Ubuntu is: /usr/lib/jvm/java-17-openjdk-amd64
Check that the Java runtime and compiler files exist:
ls -l /usr/lib/jvm/java-17-openjdk-amd64/bin/java
ls -l /usr/lib/jvm/java-17-openjdk-amd64/bin/javac
Important: If your system uses a different directory, substitute the actual path in the following steps. Do not use the example path without verifying it.
Configure JAVA_HOME
JAVA_HOME is an environment variable used by Java tools and application servers to locate the JDK. It should point to the JDK directory itself—not to the bin directory or the java executable.
Inspect the active Java alternative
sudo update-alternatives --config java
This command lists the Java runtime choices managed by Ubuntu and shows which one is selected. It helps verify the active java command; JAVA_HOME must still be set separately.
Edit the system environment file
sudo nano /etc/environment
Add or update this line, using the actual JDK path discovered above: JAVA_HOME=”/usr/lib/jvm/java-17-openjdk-amd64″
Keep existing entries intact and avoid duplicate JAVA_HOME definitions. Save and exit nano using Ctrl+O, Enter, then Ctrl+X.
Apply and confirm the setting
For the current terminal session, the video uses:
source /etc/environment
Then check the value:
echo "$JAVA_HOME"
Environment note: /etc/environment is normally read by the login/session environment system and is not a shell script. Sourcing it may work for simple assignments, but the reliable way to apply system-wide changes to new sessions is to log out and log back in, or open a fresh login session.
Final Verification
Run these commands to verify the environment variable, runtime, compiler, and Java executable directly under JAVA_HOME:
echo "$JAVA_HOME"
java -version
javac -version
"$JAVA_HOME/bin/java" -version
Expected results
- JAVA_HOME points to the actual Java 17 installation directory.
- java -version reports Java 17 if Java 17 is selected as the active default.
- javac -version reports version 17 when the matching JDK compiler is installed and on PATH.
- The final command runs Java directly from JAVA_HOME and reports Java 17.
Important distinction: JAVA_HOME does not automatically change which java command runs. If another Java version is active, use update-alternatives to select Java 17 when that is appropriate for your system.
Common Issues
| Issue | What to check |
| java: command not found | Confirm installation completed, then check command -v java and update-alternatives. |
| javac: command not found | Verify that openjdk-17-jdk is installed, rather than only a runtime package. |
| JAVA_HOME is empty | Check /etc/environment and start a new login session if needed. |
| JAVA_HOME points to a missing directory | List /usr/lib/jvm and use the actual JDK directory. |
| java -version shows another major version | Inspect sudo update-alternatives –config java and select Java 17 if required. |
| Sourcing /etc/environment fails | Check its syntax; use a fresh login session to apply system environment settings normally. |
Verification Checklist
- Ubuntu version checked.
- OpenJDK 17 JDK installed successfully.
- Java runtime and compiler versions verified.
- Actual JDK installation path confirmed.
- JAVA_HOME configured in /etc/environment.
- Java 17 selected as the active default if required.
- The Java executable under JAVA_HOME runs successfully.
Completion
Java 17 setup is complete when the JDK is installed, JAVA_HOME points to the correct JDK directory, and the final verification commands run successfully. The environment is then ready for development tools and applications that require Java 17. Confirm the Java requirements of your target application before proceeding.