Saturday, May 27, 2017

Java Installation

Before we can begin writing our first Java program, we need to make sure that the Java Development Kit (JDK) is installed on our machine. Remember from the last blog post that there are two main applications that are responsible for running Java code, the JDK and the JRE. The JDK takes the Java program that we write and translates it into an intermediate language called bytecode. The Java Runtime Environment (JRE) takes bytecode and converts it to machine language, which is run on the computer. Since the JDK includes the JRE, we only need to install the JDK. There are several ways to install the Java Development Kit, which are described below.

Method One - Install JDK from Oracle Downloads Page


The most common method used to install the JDK is by going to the Oracle downloads page. To do this, search for "download jdk" and select the first result, which should take you to the Oracle JDK download page. Oracle is the company that maintains the Java programming language. You should see a section on the page similar to the following after going there:



Please note that Oracle recently changed the installer page, and you may see a section like this:



In this case, click the "Java Download" button to get redirected to the actual installer page.

Before downloading the JDK, you must select the "Accept License Agreement" button and choose the version of the installer for your operating system. The x86 and x64 options are based on whether your machine is 32 bit (x86) or 64 bit (x64). Most modern computers are 64 bit, so you should be able to select the x64 option. If you are using Linux, you will need to unzip the installer using a command such as "tar -xvzf jdk-8u131-linux-x64.tar.gz". For Mac and Windows users, simply run the .dmg or .exe file that was downloaded. After running the installer, you should see an installation wizard similar to the following:



Follow the instructions on the wizard to complete the installation. If you're using Windows, you will also want to update the PATH environment variable, so you're able to call java from the command line. The Oracle documentation gives the steps needed to do this:

1. Click Start, then Control Panel, then System.

2. Click Advanced, then Environment Variables.

3. Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:

C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.131\bin

The entries in the PATH variable are separated by a semicolon, so you can add ";C:\Program Files\Java\jdk1.8.131\bin" to the existing entries. Make sure that you replace jdk1.8.131 with the version that you installed.

Now that the JDK is set up and configured, you can test whether the installation was successful by using the terminal (also called command line). The terminal can be opened on Windows by pressing "Windows Key + R" and typing cmd. The terminal is under Applications -> Utilities on Mac. In both cases, you may want to create a shortcut for easy access. Once the terminal is open, type "java -version" without the quotes and press enter. You can also try "javac -version" to see if the java compiler was installed correctly. You should see output similar to the following:



If you encounter any errors during the setup, double check that all of the steps were followed, or try watching a video about the JDK installation for your operating system.

Method Two - Install JDK using SDKMAN


SDKMAN stands for Software Development Kit Manager, and it is used to easily install tools needed for Java programming. SDKMAN works well on Mac and Linux computers. For Windows computers, there is a Powershell version available, but it may be easier to install Java using Method One above. To install SDKMAN, go to the installation page at http://sdkman.io/install.html, and follow the instructions. Once the installation is complete, you can check out the Usage page to get a feel for the basic commands. In particular, the command "sdk install java" is used to get the latest version of the JDK and "sdk current java" is used to verify that the installation was successful. Pretty easy, huh! The following screenshots show how the installation process might look using SDKMAN:





In my case, I already had JDK version 1.8.0_101 installed, so SDKMAN updated it to the latest version 1.8.0_131.

At this point you should have the JDK installed on your computer. We're all set to begin writing our first Java program in the next post!

1 comment:

  1. I may recommend you to disable the firewall before starting the installation over the Internet. In some cases, the default firewall settings prohibit any automatic installations or installations over the Internet (such as installing Java over the Internet). If the firewall is not properly configured, it may in some cases block the download / installation of Java. For more instructions, watch on the website explainjava.com/category/java/ here. I found there many useful tips and articles about Java. And if both 32-bit and 64-bit browsers are used simultaneously, you must install both Java versions (32-bit and 64-bit) so that the system has a Java plug-in for both browsers. You may also try this way for setting it.

    ReplyDelete