# Java Programming/Installation

* Navigate Getting Started topic: ( v • d • e )
* Installing Java on Your Computer
* Compiling programs
* Running Java programs
* Understanding a Java program
* Java IDEs
In order to make use of the content in this book, you would need to follow along each and every tutorial rather than simply reading through the book. But to do so, you would need access to a computer with the Java platform installed on it — the Java platform is the basic prerequisite for running and developing Java code, thus it is divided into two essential pieces of software:
* the Java Runtime Environment (JRE), which is needed to run Java applications and applets;
* the Java Development Kit (JDK), which is needed to develop those Java applications and applets.
However as a developer, you would only require the JDK which comes equipped with a JRE as well.
As Java is just a programming language that allows you to program the computer, it has multiple implementations available. The most popular implementation of JDK and JRE are the "Oracle Java SE" (formerly known as Sun JDK), maintained by Oracle as a commercial release. However another similarly popular implementation is the OpenJDK, with the benefit of being free software that could distribute freely under GPL v2 without the requirement of accepting the "Oracle Binary Code License Agreement for the Java SE Platform Products and JavaFX". The third option - the GCJ, as part of the GNU Compiler Collection, would also supply the JDK and JRE.
Given below are installation instruction for the Oracle Java SE JDK for various operating systems:

#### Availability check for JRE

The Java Runtime Environment (JRE) is necessary to execute Java programs. To check which version of Java Runtime Environment (JRE) you have, follow the steps below.
| 1. | For Windows Vista or Windows 7, click Start › Control Panel › System and Maintenance › System. |
| | For Windows XP, click Start › Control Panel › System. |
| | For Windows 2000, click Start › Settings › Control Panel › System. |
| | Alternatively, you can also press ⊞ Win+R to open the Run dialog. With the dialog open, type cmd at the prompt: Figure 1.1: Run dialog |
| 2. | In the command window with black background graced with white text, type the following command: JRE availability check java -version If you get an error, such as: Other output error Bad command or file name ..then the JDK may not be installed or it may not be in your path. |
    To learn more about the Command Prompt syntax, take a look at this MS-DOS tutorial.
You may have other versions of Java installed; this command will only show the first in your PATH. You will be made familiar with the PATH environment variable later in this text. For now, if you have no idea what this is all about. Read through towards the end and we will provide you with a step-by-step guide on how to set your own environment variables.
You can use your system's file search utilities to see if there is a javac.exe executable installed. If it is, and it is a recent enough version (Java 1.4.2 or Java 1.5, for example), you should put the bin directory that contains javac in your system path. The Java runtime, java, is often in the same bin directory.
If the installed version is older (i.e. it is Java 1.3.1 or Java 1.4.2 and you wish to use the more recent Java 5 release), you should proceed below with downloading and installing a JDK.
It is possible that you have the Java runtime (JRE), but not the JDK. In that case the javac program won't be found, but the java -version will print the JRE version number.

#### Availability check for JDK

Some Windows based systems come built-in with the JRE, however for the purposes of writing Java code by following the tutorials in this book, you would require the JDK nevertheless. The Java Development Kit (JDK) is necessary to build Java programs. First, check to see if a JDK is already installed on your system. To do so, first open a command window and execute the command below.
* Availability check
* javac -version
If the JDK is installed and on your executable path, you should see some output which tells you the command line options. The output will vary depending on which version is installed and which vendor provided the Java installation.

#### Advanced availability check options on Windows platform

On a machine using the Windows operating system, one can invoke the Registry Editor utility by typing REGEDIT in the Run dialog. In the window that opens subsequently, if you traverse through the hierarchy HKEY_LOCAL_MACHINE > SOFTWARE > JavaSoft > Java Development Kit on the left-hand.
The resultant would be similar to figure 1.2, with the only exception being the version entries for the Java Development Kit. At the time of writing this manuscript, the latest version for the Java Development Kit available from the Internet was 1.7 as seen in the Registry entry. If you see a resultant window that resembles the one presented above, it would prove that you have Java installed on your system, otherwise it is not.
* Caution should be exercised when traversing through the Registry Editor. Any changes to the keys and other entries may change the way your Windows operating system normally works. Even minor changes may result into catastrophic failures of the normal working of your machine. Better that you don't modify or tend to modify anything whilst you are in the Registry Editor.

#### Download instructions

To acquire the latest JDK (version 7), you can manually download the Java software from the Oracle website.
For the convenience of our readers, the following table presents direct links to the latest JDK for the Windows operating system.
| Operating system | Setup Installer | License |
| Windows x86 | Download | Oracle Binary Code License Agreement |
| Windows x64 | Download | Oracle Binary Code License Agreement |
You must follow the instructions for the setup installer wizard step-by-step with the default settings to ensure that Java is properly installed on your system. Once the setup is completed, it is highly recommended to restart your Windows operating system.
If you kept the default settings for the setup installer wizard, your JDK should now be installed at C:\Program Files\Java\jdk1.7.0_01. You would require the location to your bin folder at a later time — this is located at C:\Program Files\Java\jdk1.7.0_01\bin It may be a hidden file, but no matter. Just don't use Program Files (x86)\ by mistake unless that's where the files were installed by Java.

#### Updating environment variables

In order for you to start using the JDK compiler utility with the Command Prompt, you would need to set the environment variables that points to the bin folder of your recently installed JDK. To set permanently your environment variables, follow the steps below.
| 1. | To open System Properties dialog box use, the Control Panel or type the following command in the command window: System properties rundll32 shell32.dll,Control_RunDLL sysdm.cpl |
| 2. | Navigate to the Advanced tab on the top, and select Environment Variables... |
| 3. | Under System variables, select the variable named Path and click Edit... |
| 4. | In the Edit System Variable dialog, go to the Variable value field. This field is a list of directory paths separated by semi-colons (;). |
| 5. | To add a new path, append the location of your JDK bin folder separated by a semi-colon (;). |
| 6. | Click OK on every opened dialog to save changes and get past to where you started. |

#### Start writing code

Once you have successfully installed the JDK on your system, you are ready to program code in the Java programming language. However, to write code, you would need a decent text editor. Windows comes with a default text editor by default — Notepad. In order to use notepad to write code in Java, you need to follow the steps below:
| 1. | Click Start › All Programs › Accessories › Notepad to invoke the application. |
| | Alternatively, you can also press ⊞ Win+R to open the Run dialog. With the dialog open, type the following command at the prompt: Notepad launching notepad |
| 2. | Once the Notepad application has fired up, you can use the editor to write code for the Java programming language. |

#### Availability check for JRE

The Java Runtime Environment (JRE) is necessary to execute Java programs. To check which version of JRE you have, follow the steps below.
| 1. | Open the Terminal window. |
| 2. | Type the following command: JRE availability check java -version If you get something like this: Output on a particular Kubuntu 12.10 installation, with OpenJDK as the provider of JDK and JRE java version "1.7.0_09" OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.10.1) OpenJDK Client VM (build 23.2-b09, mixed mode, sharing) ... then a JRE is installed. If you get an error, such as: Output error java: command not found ... then the JDK may not be installed or it may not be in your PATH. |
You may have other versions of Java installed; this command will only show the first in your PATH. You will be made familiar with the PATH environment variable later in this text. For now, if you have no idea what this is all about, read through towards the end and we will provide you with a step-by-step guide on how to set your own environment variables.
You can use your system's file search utilities to see if there is a javac executable installed. If it is, and it is a recent enough version, you should put the bin directory that contains javac in your system PATH. The Java runtime, java, is often in the same bin directory.
If the installed version is older (i.e. it is Java 5 and you wish to use the more recent Java 7 release), you should proceed below with downloading and installing a JDK.
It is possible that you have the Java runtime (JRE), but not the JDK. In that case the javac program won't be found, but the java -version will print the JRE version number.

#### Availability check for JDK

The Java Development Kit (JDK) is necessary to build Java programs. For our purposes, you must use a JDK. First, check to see if a JDK is already installed on your system. To do so, first open a terminal window and execute the command below.
* Availability check
* javac -version
If the JDK is installed and on your executable path, you should see some output which tells you the command line options. The output will vary depending on which version is installed and which vendor provided the Java installation.

#### Installation using Terminal

Downloading and installing the Java platform on Linux machines is very easy and straightforward. You have two choices to install the Java platforms: using a package manager such as DPKG/APT, YUM/RPM etc., or directly install them using the binary package. To use the terminal to download and install the Oracle Java SE platform, follow the instructions below.
| 1. | Open the Terminal window. |
| 2. | At the prompt, type in the line followed by the name of your package manager as shown below: Retrieving the java packages # APT - Ubuntu, Linux Mint $ sudo add-apt-repository ppa:webupd8team/java $ sudo apt update $ sudo apt-get install oracle-java8-installer # APT - Debian, etc. $ echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/webupd8team-java.list $ echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886 $ sudo apt-get update $ sudo apt-get install oracle-java8-installer # Portage - Gentoo # You need to accept the license and fetch the source code manually from http://www.oracle.com/technetwork/java/javase/downloads/index.html # and save it to /usr/portage/distfiles $ echo "dev-java/oracle-jdk-bin Oracle-BCLA-JavaSE" | sudo tee -a /etc/portage/package.license # Accept the Oracle License $ emerge oracle-jdk-bin # Pacman - Arch $ sudo pacman -S jdk8-openjdk |
| 3. | All Java softwares should be installed and instantly available now. |

#### Download instructions

Alternatively, you can manually download the Java software from the Oracle website.
For the convenience of our readers, the following table presents direct links to the latest JDK for the Linux operating system.
| Operating system | RPM | Tarball | License |
| Linux x86 | Download | Download | Oracle Binary Code License Agreement |
| Linux x64 | Download | Download | Oracle Binary Code License Agreement |

#### Start writing code

The most widely available text editor on GNOME desktops is Gedit, while on the KDE desktops, one can find Kate. Both these editors support syntax highlighting and code completion and therefore are sufficient for our purposes.
However, if you require a robust and standalone text-editor like the Notepad++ editor on Windows, you would require the use of the minimalistic editor loaded with features – SciTE. Follow the instructions below if you wish to install SciTE:
| 1. | Open the Terminal window. |
| 2. | At the prompt, write the following: Retrieving the java packages $ sudo apt-get install scite |
| 3. | You should now be able to use SciTE for your programming needs. You may also want to try Geany. Installation instructions are similar to those for SciTE. |
On Mac OS, both the JRE and the JDK are already installed. However, the version installed was the latest version when the computer was purchased, so you may want to update it.

#### Updating Java for Mac OS

* Go to the Java download page.
* Mechanically accept Oracle's license agreement.
* Click on the link for Mac OS X.
* Run the installer package.

#### Availability check for JDK

The Java Development Kit (JDK) is necessary to build Java programs. For our purposes, you must use a JDK. First, check to see if a JDK is already installed on your system. To do so, first open a terminal window and execute the command below.
* Availability check
* java -version
If the JDK is installed and on your executable path, you should see some output which tells you the command line options. The output will vary depending on which version is installed and which vendor provided the Java installation.
If you already have the JRE installed, you can use the Java Wiki Integrated Development Environment (JavaWIDE) to code directly in your browser, no account or special software required.
Click here to visit the JavaWIDE Sandbox to get started.
For more information, click here to visit the JavaWIDE site.