Install and switch between multiple Javas on Ubuntu

--Describes how to install multiple java and switch. --Implemented on Ubuntu 16.04. --Install java8 and java9 and try switching.

manner

Search for java to install

$ sudo apt search openjdk-.*-jdk$

openjdk-8-jdk/xenial-updates,xenial-security 8u252-b09-1~16.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-9-jdk/xenial 9~b114-0ubuntu1 amd64
  OpenJDK Development Kit (JDK)

Java installation

installation of java8

$ sudo apt install openjdk-8-jdk

installation of java9

$ sudo apt install openjdk-9-jdk

Check java version

$ java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~16.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

About update-alternatives (before switching)

--java under / usr / bin is a symbolic link and points to java under / etc / alternatives. --The java under / etc / alternatives is also a symbolic link, and in the example below, it points to the java in / usr / lib / jvm / java-8-openjdk-amd64 / jre / bin. --update-alternatives can point to different versions of java by rewriting the symbolic links under / etc / alternatives. (Update-alternatives can be used in addition to java)

$ which java
/usr/bin/java

$ ls -la /usr/bin/java
lrwxrwxrwx 1 root root 22 May  5 01:46 /usr/bin/java -> /etc/alternatives/java

$ ls -la /etc/alternatives/java
lrwxrwxrwx 1 root root 46 May  5 07:52 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Java switching

I will list three methods I tried, but I personally recommend method 3.

(Method 1) Switch with update-alternatives (* Switching is halfway)

--The installed java is displayed by the update-alternatives command, so specify the Selection number to switch. --The java currently used by the java with an asterisk. -Select 2 and switch to java9.

$ 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-8-openjdk-amd64/jre/bin/java   10        auto mode
* 1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   10        manual mode
  2            /usr/lib/jvm/java-9-openjdk-amd64/bin/java       10        manual mode

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

--If you get the following message, update-alternatives only recognizes java8, so you need to register java9.

$ sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Nothing to configure.

--After executing the following registration command, switch again. (The last number is priority.)

sudo update-alternatives --verbose --install /usr/bin/java java /usr/lib/jvm/java-9-openjdk-amd64/jre/bin/java 10

--You can see that the java version has been switched.

$ java -version
openjdk version "9-internal"
OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src)
OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode)

――But if you look at the symbolic links in / etc / alternatives, you can see that only the java command is switched. .. .. update-alternatives is not a java-only command, so it's natural. (Actually, there are many more java related commands in / etc / alternatives.)

$ ls -ltr /etc/alternatives/java*
lrwxrwxrwx 1 root root 45 May  5 08:03 /etc/alternatives/javadoc -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javadoc
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javah -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javah
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javac -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javac
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javap -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javap
lrwxrwxrwx 1 root root 42 May  5 08:04 /etc/alternatives/java -> /usr/lib/jvm/java-9-openjdk-amd64/bin/java

(Method 2) Use update-java-alternatives (* Switching is halfway)

--Use update-java-alternatives, a switching command dedicated to java. --However, it is necessary to register each java version on the update-alternatives side as well. (Because update-alternatives is used in update-java-alternatives.) --Register the command described in jinfo in update-alternatives with one liner

$ cd /usr/lib/jvm
$ cat .java-1.9.0-openjdk-amd64.jinfo | grep -v -e '=' -e '^$' | awk '{system("sudo update-alternatives --verbose --install /usr/bin/" $2 " "$2 " " $3 " " 10)}'

--Try to display java that can be switched.

$ update-java-alternatives --list
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64
java-1.9.0-openjdk-amd64       1091       /usr/lib/jvm/java-1.9.0-openjdk-amd64

--Try switching to java9.

$ sudo update-java-alternatives --set java-1.9.0-openjdk-amd64

--After all, update-java-alternatives doesn't switch everything. .. ..

$ ls -ltr /etc/alternatives/java*
lrwxrwxrwx 1 root root 45 May  5 08:03 /etc/alternatives/javadoc -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javadoc
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javah -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javah
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javac -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javac
lrwxrwxrwx 1 root root 43 May  5 08:03 /etc/alternatives/javap -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javap
lrwxrwxrwx 1 root root 42 May  5 08:24 /etc/alternatives/java -> /usr/lib/jvm/java-9-openjdk-amd64/bin/java

(Method 3) Switch with update-alternatives -Part 2- (* Recommended)

--In the end, do you have to use update-alternatives to switch all java related commands? .. .. --Registration to update-alternatives is the same as the procedure performed in Part 2.

$ cd /usr/lib/jvm
$ cat .java-1.9.0-openjdk-amd64.jinfo | grep -v -e '=' -e '^$' | awk '{system("sudo update-alternatives --verbose --install /usr/bin/" $2 " "$2 " " $3 " " 10)}'

--update-Switch java related commands in alternatives with one liner

$ cat .java-1.9.0-openjdk-amd64.jinfo | grep -v -e '=' -e '^$' | awk '{system("sudo update-alternatives --set " $2 " " $3 )}'

--Confirm that all java related commands have been switched.

$ ls -ltr /etc/alternatives/java*
lrwxrwxrwx 1 root root 42 May  5 08:24 /etc/alternatives/java -> /usr/lib/jvm/java-9-openjdk-amd64/bin/java
lrwxrwxrwx 1 root root 43 May  5 08:42 /etc/alternatives/javac -> /usr/lib/jvm/java-9-openjdk-amd64/bin/javac
lrwxrwxrwx 1 root root 43 May  5 08:42 /etc/alternatives/javah -> /usr/lib/jvm/java-9-openjdk-amd64/bin/javah
lrwxrwxrwx 1 root root 45 May  5 08:42 /etc/alternatives/javadoc -> /usr/lib/jvm/java-9-openjdk-amd64/bin/javadoc
lrwxrwxrwx 1 root root 43 May  5 08:42 /etc/alternatives/javap -> /usr/lib/jvm/java-9-openjdk-amd64/bin/javap

Remarks

――Neither of the above methods will switch JAVA_HOME, so you need to switch individually. --Although it is described in jinfo, there are some files that do not actually exist. In that case, ʻupdate-alternatives: error: alternative path / usr / lib / jvm / java-9-openjdk-amd64 / bin / appletviewer doesn' An error such as t exist` occurs, but there seems to be no problem.

Recommended Posts

Install and switch between multiple Javas on Ubuntu
Install JDK and JRE on Ubuntu 16.10
Switch between GUI and CUI on CentOS6.
Build and install Wireshark Development Release (3.3.1) on Ubuntu
Install pyqt5 on ubuntu
Install Ruby on Ubuntu 20.04
Switch between multiple versions of Java on Mac
Switch between JDK 7 and JDK 8
How to install multiple JDKs on Ubuntu 18.04 LTS
Install Autoware on Ubuntu 18.04.5
Install Homebrew on Ubuntu 20.04
Install rbenv with apt on ubuntu and put ruby
Install OpenJDK7 (JAVA) on ubuntu 14.04
Install Cybozu Office 10 on Ubuntu 20.4
Install Docker on Ubuntu Server 20.04
Install zabbix agent (5.0) on Ubuntu 18.04
Install MAV Proxy on Ubuntu 18.04
Install Arudino IDE on Ubuntu 20
Install Java on WSL Ubuntu 18.04
Install Ubuntu Desktop 20.10 on RaspberryPi4
Install Arduino IDE on Ubuntu 20.04
Install raspi-config on Ubuntu 20.04 (LTS)
Difference between bundle and bundle install
Install WordPress 5.5 on Ubuntu 20.04 LTS
Install PlantUML on Intellij on Ubuntu
Install Ubuntu Server 20.04 on Btrfs
Note: Install PostgreSQL 9.5 on Ubuntu 18.04
Install docker and docker-compose on ubuntu in the shortest process
Install AWS IoT Greengrass on Ubuntu
Install ngrok on ubuntu16.04 using Vagrant
Install and configure jenv on macOS
Install MQTT broker Mosquitto on Ubuntu 18.04
Enable Java 8 and Java 11 SDKs on Ubuntu
Install SonarQube on ECS instance on Ubuntu 16.04
Install ruby on Ubuntu 20.04 with rbenv
Install Java 9 on windows 10 and CentOS 7
Installing and using Ansible on Ubuntu 16.04
How to install and use Composer on an ECS instance on Ubuntu 16.04
How to install and configure the monitoring tool "Graphite" on Ubuntu
Install Ubuntu20.04 on RaspberryPi 4 and build Kubernetes to run the container
Install Ubuntu Server 20.04 in VirtualBox on Mac and connect with SSH
Install Docker on Ubuntu and set up remote connection using tls
Upgrade and switch Ruby versions on Windows 10
Install docker and docker-compose on Alpine Linux
[Memo] Difference between bundle install and update
Headless install of Ubuntu 20.10 on Raspberry Pi
Install OpenJDK (Java) on the latest Ubuntu
How to install production Metabase on Ubuntu
I want to install PHP 7.2 on Ubuntu 20.04.
Install Realtek network driver on Ubuntu Note
Notes on Java's Stream API and SQL
How to switch between multiple Java versions
Wine settings on Ubuntu 18.04 (install WOLF RPG Editor)
Install Ubuntu Core 18 on Raspberry Pi 2 Model B
Publish MySQL externally and log in on Ubuntu
Install Docker on Raspberry Pi 4 and Raspberry Pi OS 64bit
How to install network drivers on standalone Ubuntu
Build a DHCP and NAT router on Ubuntu 16.04
How to install NVIDIA driver on Ubuntu 18.04 (Note)
Install SonarQube on Mac and challenge source analysis
Docker on Ubuntu18.04 on WSL2 and VSCode installation instructions