0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

How to install the latest openjdk/jre using apt on Ubuntu

Last updated at Posted at 2021-07-04

Firstly, Check whether your environment has already installed Java or not:

[Ubuntu: ~]$apt list --installed | grep -i "jdk"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

openjdk-8-jdk-headless/focal-updates,focal-security,now 8u292-b10-0ubuntu1~20.04 amd64 [installed,automatic]
openjdk-8-jdk/focal-updates,focal-security,now 8u292-b10-0ubuntu1~20.04 amd64 [installed]
openjdk-8-jre-headless/focal-updates,focal-security,now 8u292-b10-0ubuntu1~20.04 amd64 [installed,automatic]
openjdk-8-jre/focal-updates,focal-security,now 8u292-b10-0ubuntu1~20.04 amd64 [installed,automatic]

Since openjdk-8-jdk/jre is too old, uninstall the all old openjdk/jre:

[Ubuntu: ~]$sudo apt --purge remove openjdk-8-jdk
[Ubuntu: ~]$sudo apt --purge remove openjdk-8-jre
[Ubuntu: ~]$sudo apt --purge remove openjdk-8-jdk-headless
[Ubuntu: ~]$sudo apt --purge remove openjdk-8-jre-headless
...

Install jdk as follows:

[Ubuntu: ~]$sudo apt install default-jdk

Check the installed version:

[Ubuntu: ~]$java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

For setting JAVA_HOME, you need to make sure where the jdk/jre is installed by apt.

[Ubuntu: ~]$sudo update-alternatives --list java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

/usr/lib/jvm/java-11-openjdk-amd64 is JAVA_HOME path. Because you already got the path where JDK has been installed as the above, set JAVA_HOME and PATH:

[Ubuntu: ~]$echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
[Ubuntu: ~]$echo 'PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc
[Ubuntu: ~]$grep -i java ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
PATH=$PATH:$JAVA_HOME/bin

Apply the path:

[Ubuntu: ~]$source ~/.bashrc
[Ubuntu: ~]$env | grep -i "java"
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...
:/usr/lib/jvm/java-11-openjdk-amd64/bin
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?