Skip to content

Instantly share code, notes, and snippets.

@ofhouse
Created October 13, 2020 15:16
Show Gist options
  • Save ofhouse/b6cca8006329107fa0770a8dd87f45cf to your computer and use it in GitHub Desktop.
Save ofhouse/b6cca8006329107fa0770a8dd87f45cf to your computer and use it in GitHub Desktop.
Install and manage multiple Java SDK versions on Ubuntu

Install and manage multiple Java versions on Ubuntu

This guide shows per example the installation of the Java SDK versions 8 (LTS, already installed) and 11 (LTS).

1. Check which Java versions are avaialable or already installed

apt --names-only search "openjdk-.*jre$"

> Sorting... Done
  Full Text Search... Done
  openjdk-11-jre/focal-updates,focal-security 11.0.8+10-0ubuntu1~20.04 amd64
    OpenJDK Java runtime, using Hotspot JIT

  openjdk-13-jre/focal-updates 13.0.3+3-1ubuntu2 amd64
    OpenJDK Java runtime, using Hotspot JIT

  openjdk-14-jre/focal-updates 14.0.1+7-1ubuntu1 amd64
    OpenJDK Java runtime, using Hotspot JIT

  openjdk-8-jre/focal-updates,focal-security,now 8u265-b01-0ubuntu2~20.04 amd64 [installed,automatic]
    OpenJDK Java runtime, using Hotspot JIT

Thanks to: https://askubuntu.com/a/1112189/393623

2. Install additional Java SDKs

Since the openjdk-8-jre is already installed we only need to install the openjdk-11-jre package:

sudo apt install openjdk-11-jre

3. Make sure that you are using the correct Java version

Open a terminal and run java -version:

java -version

# Output for OpenJDK 8
> openjdk version "1.8.0_265"
  OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~20.04-b01)
  OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

# Output for OpenJDK 11
> openjdk version "11.0.8" 2020-07-14
  OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
  OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

If you see the output for the OpenJDK 8 but you want to use the OpenJDK 11 (e.g. for starting eclipse) you can easily switch between the different versions:

sudo update-alternatives --config java

> There are 2 choices for the alternative java (providing /usr/bin/java).

    Selection    Path                                            Priority   Status
  ------------------------------------------------------------
  * 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
    1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
    2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

  Press <enter> to keep the current choice[*], or type selection number:

After selecting the version you can run the java -version command again in your terminal to verify that the selected Java SDK is now being used.

@asgs
Copy link

asgs commented Apr 14, 2023

this guide is very clear and concise. Thank you so much. Folks, alternatives is the way to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment