How To Install And Switch Between Alternative Java Versions
Java is a general purpose computer programming language. Sometimes you have to work with different versions of java at the same time. In such cases you have to switch between versions.
First lets see how to install java on your Ubuntu machine.
Step 1
Check whether your Ubuntu is 32-bit or 64-bit
file /sbin/init
Or you can go to settings and check your Ubuntu version.
Step 2
Next download a Java version from Oracle site
Step 3
Set the path of the terminal to where you downloaded the zip file
Step 4
Use the following command to extract the downloaded file
sudo tar -xvf <your file name>.tar.gz
Step 5
Open /etc/profile file. To do it use the following command
sudo nano /etc/profile
or
sudo gedit /etc/profile
Now enter the following function to the etc/profile file.
JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0PATH=$PATH:$HOME/bin:$JAVA_HOME/binexport JAVA_HOMEexport PATH
If this not working, enter the following function replacing upper one
export JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0export PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
Step 6
Now reload the environment
. /etc/profile
Step 7
Now give the location of Java to Ubuntu using following commands
sudo update-alternatives — install “/usr/bin/java” “java” “<Directory where JAVA has been extracted>/bin/java” 1sudo update-alternatives — install “/usr/bin/javac” “javac” “<Directory where JAVA has been extracted>/bin/javac” 1sudo update-alternatives — install “/usr/bin/javaws” “javaws” “<Directory where JAVA has been extracted>/bin/javaws” 1
Step 8
Now set the installed version as the default version of java
sudo update-alternatives — set java <Directory where JAVA has been extracted>/bin/javasudo update-alternatives — set javac <Directory where JAVA has been extracted>/bin/ javacsudo update-alternatives — set javaws <Directory where JAVA has been extracted>/bin/javaws
Step 9
Verify your Java version with the following command
java -versionjavac -version
Now lets install another version of java to Ubuntu machine. In this time lets install openJDK 8 version.
Step 1
get update your apt
sudo apt-get update
Step 2
Now install openJDK 8
sudo apt-get install openjdk-8-jre
Step 3
Set the variables
echo “JAVA_HOME$(which java)” | sudo tee -a /etc/environment
Step 4
Reload the system variables
source /etc/environment
Step 5
Verify variables using this
echo $JAVA_HOME
Now you can check the version of java. You can see that it shows the version as older version(one you installed earlier). But if you need to use version 8, how do you switch to that version. You can use following commands to switch between alternative versions.
sudo update-alternatives — config java
When you run this in terminal it shows us what are the available versions that you can switch on.You can choose the number and after you confirm it, it will be your default java version. You can change your java compiler version with following command.
sudo update-alternatives — config javac