Do note that the following method should work on all Ubuntu Desktops, as well as on Ubuntu-based distros like Linux Mint. There are multiple versions and types of Java one can install on his/her computer which can confuse beginners sometimes. However, if you are not sure which Java version you have to install, you can go safe with the default variant.
Installing OpenJDK/JRE For those who don’t know, OpenJDK is an open-source variant of JDK and JRE.Simply enter the following commands one-by-one in the terminal and you should be good to go. You can open the terminal by pressing CTRL+ALT+T simultaneously.
$ sudo apt update
Before we begin with the installation, it is also necessary to check if you already have Java installed. YOu can check the current version of Java on your system using the following command
$ java -version
If you don’t see any java installation, please process to the next steps.
Enter this command to install java:
$ sudo apt install default-jre $ sudo apt install default-jdk
To check if Java has been installed or not, you can use the same command we mentioned earlier as well $ java -version
To check the JDK installation, one can use the following command $ javac -version
$ javac -version
You have successfully installed the open-source Java variant which is provided by Ubuntu itself. However, if you are interested in getting the official one by Oracle, you can follow the steps mentioned below.
Do note that, Oracle JDK simply cannot be installed using any package manager in the terminal directly as it goes against Oracle’s licensing agreement. You will have to create an account and download from their website.
While it is a bit complex stuff, just keep following the steps and you should be good to go
$ scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz username@your_server_ip:~
Don’t forget the replace “downloads” with your folder name where the file was downloaded, “username” with your username and “your_server_ip” with your IP address.
Once done, execute the following command
$ sudo apt install software-properties-common
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
You should see an output something like this:
$ gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported $ gpg: Total number processed: 1 $ gpg: imported: 1
$ sudo add-apt-repository ppa:linuxuprising/java
$ sudo mkdir -p /var/cache/oracle-jdk11-installer-local/ $ sudo cp jdk-11.0.7_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
$ sudo apt install oracle-java11-installer-local
The installer might also ask to accept the Oracle License agreement. Simply accept it.
Also Read: Dual boot Ubuntu and Windows 10
Do note that if you have multiple Java installations on one serve or machine, you will have to configure the version to use as the default
$ sudo update-alternatives --config java
Enter the Selection Number to set your default Java installation.
Setting the JAVA_HOME Environment Variable
A lot of programs that are written using Java uses the “JAVA_HOME” environment variable to check and determine the installation locations on your machine.
In order to set the environment variable, we will have to determine the installation location of Java.
From the output, you can determine your Java installation path.
Output
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-11-oracle/bin/java 1091 manual mode
Press to keep the current choice[*], or type selection number:
In this case, the installation paths are as follows:
Copy your installation path and open /etc/environment/ using vim or your preferred text editor. Nano would work completely fine too.
$ sudo vi /etc/environment
Simply add the following line tho the end of your file and don’t forget to replace “your installation path” with your actually installation path which you copied earlier.
JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-amd64″
Save the file and reload it by executing the following command.
$ source /etc/environment
This will enable the JAVA_HOME path for all users on your machine.
You can also execute the mentioned code on the terminal and confirm Path installation. echo $JAVA_HOME
echo $JAVA_HOME
So that way you can install multiple java versions and manage them. Softwares like Tomcat which specifically runs on Java will not have any issues running on your machine now.
Also Read: How to Install Chrome on Ubuntu
Your email address will not be published. Required fields are marked *
Comment
Your name
Email address
Save my name, email, and website in this browser for the next time I comment.