Quick overview and prerequisites
Tired of Java hiding on your Ubuntu box like it owes you money? This guide gets a JDK installed, verifies it, and sets the system default when multiple versions show up for dinner. We use apt and update alternatives and there is an optional path for vendor builds. No drama, just working Java.
Refresh apt and install OpenJDK
First make sure the package list is up to date so apt knows what is available. Run this like you mean it.
sudo apt update
Then install a JDK package for development. Use OpenJDK 17 for a modern runtime or OpenJDK 11 for long term support. Example commands:
sudo apt install openjdk-17-jdk
# or
sudo apt install openjdk-11-jdk
Apt will pull dependencies and install binaries in the usual places so you can stop guessing where java lives.
Verify the installation
Check both the runtime and the compiler so you do not get surprised during the build.
java -version
javac -version
The version and vendor will appear in the output so you know which Java is actually working for you.
Select the default Java with update alternatives
If you have multiple Java versions installed tell the system which one to use. The update alternatives tool is the polite way to do this.
sudo update-alternatives --config java
sudo update-alternatives --config javac
Pick the number that matches the path you want. Run both commands so the runtime and compiler agree. Yes this step matters and yes it will save you time later.
Optional vendor builds and Oracle JDK
Some older or vendor specific apps demand a particular JDK build. Download the Debian package from the vendor site and install it with dpkg or use a trusted PPA for easier updates. Example:
sudo dpkg -i package.deb
sudo apt install -f
Only follow this route when you have to. Most modern servers and dev machines are perfectly happy with OpenJDK.
Set JAVA_HOME and common tips
If Java still behaves like a cryptic puzzle check environment variables such as JAVA_HOME and the system PATH. A common place to set JAVA_HOME system wide is the environment file.
sudoedit /etc/environment
# add or update
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
Then log out or open a new shell or run source /etc/environment
to pick up the change.
Final notes and recommendations
Prefer LTS releases for production servers and make sure your build tools and runtime match. After these steps your Ubuntu machine should have a working JDK ready to compile and run Java applications. If things still misbehave audit PATH entries and remove the ones that are pointing at ancient versions. You earned this tiny win.