--Describes how to install multiple java and switch. --Implemented on Ubuntu 16.04. --Install java8 and java9 and try switching.
$ 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)
$ sudo apt install openjdk-8-jdk
$ sudo apt install openjdk-9-jdk
$ 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)
--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
I will list three methods I tried, but I personally recommend method 3.
--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
--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
--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
――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