Table of Contents
Introduction to Java programming language
Java is a programming language and computing platform which was first released by Sun Microsystems in 1995. Java is a programming language built for the age of the Internet. There are lots of applications and websites that will not work unless you have Java installed. Java is a fast, secure, and reliable programming language. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!. In this tutorial, we will get a start to Java by installing it on our local PC.
We will learn step by step how to install java on Linux/Ubuntu, Windows, and Mac. By the end of this tutorial, you will be able to successfully install java on your local PC.
Setup up Java environment on Linux
In this section, we will learn how we can install and set the java path variable by using command lines. We can get Java on Ubuntu via multiple packages. These are JRE, OpenJDK, and Oracle JDK. See the list below which explains each of these packages.
- JRE (Java Runtime Environment) is what we would need normally to run a Java-based application. This is all you need if you are not a programmer.
- JDK (Java Development Kit) is what we need to if we have to do some programming or develop software related to Java.
- OpenJDK is the Open Source implementation of the Java Development Kit while Oracle JDK is the official Oracle version of Java Development Kit. While OpenJDK is sufficient for most of the cases, some programs such Android studio suggests using Oracle JDK to avoid UI/performance issue.
Check if Java is already installed on our PC
Before going to install java on your PC, it is recommended to check if it is already installed or not. Because if it is installed you can check which version is that.
So, to check Java on your pc, open the terminal and type the following commands in the terminal.
If java is already installed, you will get something like this as an output showing details about the version of the Java installed. In my case, the java 11.0.10 version is installed. If might get a different version if you have a different version of java installed on your pc.
java 11.0.10 2021-01-19 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)
Now, if it gives an error or couldn't recognize the word java, that means Java is not installed on your pc.
The program ‘java’ can be found in the following packages: * default-jre * gcj-4.6-jre-headless * openjdk-6-jre-headless * gcj-4.5-jre-headless * openjdk-7-jre-headless Try: sudo apt-get install
If you get an output like this one, Then you can follow these steps to install java on your pc. But in this tutorial, we will cover how we can install Official Oracle OpenJDK.
Method-1: Install java from the official website of Oracle using GUI
We can either install the java using the command line or by going to the official site and downloading manually. First, let us see how we can install it manually on Linux.
Go to the official website of Oracle and click on the download section to see which version is suitable for your PC.
Once you are in the download, you can choose the available versions for Linux depending on your PC. In my case, I had downloaded Linux x64 Debian Package. Click on it, accept the license agreement and the download should start.
It will ask to provide a path, specify a path and the download should start.
The download will appear at the bottom of the browser and once finished you can open it. Once we click on it, the following options will appear.
Choose the second option to open the software install, and click on open, at the right top corner. This will open the ubuntu software and finds the available version there, and give you the option to install it. See the picture below:
Once you click on install, it will start installing java on your PC.
Method-2: Install java using CLI and set the Path variable
Now let us see how we can install java JDK on our ubuntu using the terminal. In this section, we will be installing JDK version 16 which is the latest version available on Oracle's official website. Go to the download section and click on the download button to start downloading the java jdk version 16 for Linux. Once it is download, you can open the terminal and go to the download folder, and ls the files there.
Now type the following commands to run the JKD file.
sudo dpkg -i jdk-16.0.2_linux-x64_bin.deb
It will ask for an administrative password, and once you enter the password, the process should continue. Once the installation is complete we have to update in order to set the path to Oracle executable file. Use the following commands to update.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.2/bin/java 1
Now we have to provide the next update alternative command for javac. See the command below:
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-16.0.2/bin/javac 1
Once you are done with all these commands, you can check the java by giving the following commands.
Now let us set the JAVA_HOME Variable environment for our installation. For that, we can use the following commands which give us the location of our java installation.
sudo update-alternatives --config java
which should give you an output like this one;
update-alternatives: using /usr/lib/jvm/jdk-16.0.2/bin/java to provide /usr/bin/java (java) in manual mode
Now to set the JAVA HOME variable, enter the following command in the terminal which should open a window to create a new home variable.
sudo gedit /etc/environment
This should open the environment window:
In my case, I already have JAVA_HOME variable set for java version 11. You can see it in the picture above as well. In order to set a path for your new installation, just type the same command with the difference in the version of java. In my case, it is 11.0.10 and in your case, the path should be
/usr/lib/jvm/jdk-16.0.2, and then all should be good.
Install java with HOME variable on Windows
Now let us follow step by step to see how we can install java JDK on windows with the HOME variable. First, go the official website of oracle and download the available version for windows. Click here to go to the oracle website. Click on the exe file and download, not the zip one.
Once you click on it, it will ask to accept the license and then the download will start. The download will finish in a few minutes. Once the download is finished, click on the executable file and an introductory window will appear saying do you want to make changes, click on yes and then click on next. The following window will appear then showing the location of the file.
This will be the default path, you can change it if you want to change the default path. Then click on next and the process should proceed and install the java successfully on your windows.
Now we have to go to the installed java folder and inside the bin folder because it is the important file that contains all the important files. Copy the path so we can use that path to set our environment. In my case the folder location is as follows:
Once you have copied the path to the bin folder, then go to the windows button then click on setting and finally open the system.
Now search environment in the search bar and select the second option as shown below:
It will open a new window then click on the environment variable and it should open the following window.
Select the path and click on the edit button and add the new path of your java bin folder that you had copied. Once you pasted the directory, then your path to the java folder is ready. To set the JAVA_HOME variable again click on new button which should open a window like this one:
Remember that for the variable value, past the link up to the JDK folder ( exclude the bin one) and click
ok. Now we have successfully added
JAVA_HOME variable as well. Now open the command line and enter the following commands to confirm the installation of java on your windows.
Now, this confirms that we have successfully installed java version 16 on our windows.
Install Java on Mac
If you are using Mac operating system then follow these simple steps to install java on your Mac successfully. Again you can either install the Java JDK from the official oracle website for Mac or you can install using the command line. We have to install the JDK file with the .dmg extension for Mac.
Open the terminal and type the following commands to install Java version 11 on your mac.
Before the downloading starts, it will ask to accept the license, once you accept the license, the download will start.
When the download is finished, you can find the downloaded file of JKD in the download section and you can just double click to start the installation on your Mac.
When you double-clicked on the downloaded file, the installation application will display the introduction widow. Then click on continue and the installation type window will appear. Click on install, which will ask for the user name and password.
Once you provide the user name and password, the process will continue and will install the Java jdk successfully on your Mac.
In this tutorial, we learned how we can install java jdk on different operating systems including Linux, Windows, and Mac. We cover each process step by step along with screenshots and commands as well. All in all, this tutorial provides depth and the full installation process of java in a simple and easy way.