Since Java 8, Oracle has been in rapid update mode, releasing a release update every six months. Now that the latest version is Java 16, how do you manage multiple JDK versions in Ubuntu?
Open the terminal and enter sudo add-apt-repository ppa: openJDK -r/ppa to add the software source
$ sudo add-apt-repository ppa:openjdk-r/ppaMore info: https://launchpad.net/~openjdk-r/+archive/ubuntu/ppa Press [ENTER] to continue or Ctrl-c to cancel adding it. ... Touchtouch39.6kb in 2s (21.2 kB/s) Reading package Lists... DoneCopy the code
Enter sudo apt-get update to upgrade the system package
$ sudo apt-get update
Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
...
Reading package lists... Done
Copy the code
Enter the sudo apt-get install openJDK-14-jdk command to install the specified JDK version, in this case the version I chose is JDK14
$ sudo apt-get install openjdk-14-jdkReading package lists... Done Building dependency tree Reading state information... Done ... Processing Triggers for hicolor-icon-theme (0.17-2)... Processing Triggers for MIME - Support (3.64 Ubuntu1)...Copy the code
The update-alternatives command manages multiple JDK versions, such as switching JDK versions:
$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 manual mode
3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
Copy the code
Or set the default Java version:
/mnt/c/Users/vincent update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 auto mode
1 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
Copy the code