Let's start by assuming, you don't have any Java installed. We are going to first install 2 versions of Java; openjdk 8 and openjdk 11.
To do so open a terminal and just type this command to install openjdk8:
sudo apt install openjdk-8-jdk
The installation process should be straight forward, just choose the 'Y' option when prompted.
Once is completed, check the java version
java -versionopenjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.18.10.1-b03) OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)
Now let's install openjdk 11
sudo apt install openjdk-11-jdk
Check the version again
java -versionopenjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.10.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.10.1, mixed mode, sharing)
Now you have 2 versions of the JDK installed. You can see all the java versions you have by running this command:
update-java-alternatives --listjava-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
If you want you can ls into the jvm directory
ls -l
lrwxrwxrwx 1 root root 25 Sep 20 2018 default-java -> java-1.11.0-openjdk-amd64
lrwxrwxrwx 1 root root 21 Apr 23 2019 java-1.11.0-openjdk-amd64 -> java-11-openjdk-amd64
drwxr-xr-x 9 root root 4096 Nov 9 08:49 java-11-openjdk-amd64
lrwxrwxrwx 1 root root 20 Jan 14 2019 java-1.8.0-openjdk-amd64 -> java-8-openjdk-amd64
drwxr-xr-x 7 root root 4096 Nov 9 10:13 java-8-openjdk-amd64
lrwxrwxrwx 1 root root 25 Sep 20 2018 default-java -> java-1.11.0-openjdk-amd64
lrwxrwxrwx 1 root root 21 Apr 23 2019 java-1.11.0-openjdk-amd64 -> java-11-openjdk-amd64
drwxr-xr-x 9 root root 4096 Nov 9 08:49 java-11-openjdk-amd64
lrwxrwxrwx 1 root root 20 Jan 14 2019 java-1.8.0-openjdk-amd64 -> java-8-openjdk-amd64
drwxr-xr-x 7 root root 4096 Nov 9 10:13 java-8-openjdk-amd64
Note that there are some useful simlinks that you could use to refer to the version you want when configuring. But for the scope of this blog I will be using directly the folder names.
A way to manually change the java versions is to just run this:
sudo update-alternatives --config java [sudo] password for computername: 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:
To change the version of the compiler you can use the same command but with javac instead
sudo 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-11-openjdk-amd64/bin/javac 1111 auto mode * 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 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:
But also that's not all, the JAVA_HOME environment variable also needs to be configured. This environment variable is often used by the IDE and other Java technologies tools so it needs to be configured. As you can now see, if you wanted to quickly switch version, this would actually still be slow. Now I am going to explain how to complete the setup for q quick switch between java versions.
Make sure to create the JAVA_HOME variable configured system wide. If is not there make sure you edit that file and you add it.
cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/"
Notice that I didn't wire the JAVA_HOME into the PATH in there, the reson is because i like to do that in my local .bashrc file. In this file also I have maven, aliases and other things that I think is better not to open system wide.
cat ~/.bashrc export M2_HOME=/home/javing/maven export M2=$M2_HOME/bin export PATH=$JAVA_HOME/bin:$M2:$PATH
All we need now, is some aliases that can help us switch between java versions and at the same time configure the JAVA_HOME variable. Add this to the .bashrc files
#My aliases alias jv='java -version' alias j8='sudo update-java-alternatives -s java-1.8.0-openjdk-amd64;jv;homej8' alias j11='sudo update-java-alternatives -s java-1.11.0-openjdk-amd64;jv;homej11' alias homej8='export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/;echo $"JAVA_HOME set to:";echo $JAVA_HOME;s' alias homej11='export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/;echo $"JAVA_HOME set to:";echo $JAVA_HOME;s' alias s='source ~/.bashrc'
Note that the command update-java-alternatives will update both the compiler and the jvm versions, in my config I am using the directory name rather than the simlink. to test this just type in the terminal the aliases j8 or j11 to switch between jdk's.
Usage example:
j11 openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.10.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.10.1, mixed mode, sharing) JAVA_HOME set to: /usr/lib/jvm/java-11-openjdk-amd64/bin/
Now, you are all set and ready for Javing! ;)
No comments:
Post a Comment