Java Programming/Getting Started/Installation

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

← Java Programming Environment | Compilation →

Contents

[edit] Check for availability of Java

[edit] The Java platform

For more in-depth detail, read the section on The Java platform

Before we delve into checking for the availability of Java on our systems, we must come to terms with the Java platform itself. The Java platform in a two-faced coin. Programs written in Java need to be executed on the operating system of choice using the Java Runtime Environment or JRE but to develop the same, you would need a Java Development Kit or JDK.

[edit] 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 or a command window and execute the command below.

javac -version

Don't worry if you have no idea as to how to invoke a command window. On a Windows platform, one can invoke a command window simply by clicking on the Start menu and then clicking the Run... button. This would bring forth the Run dialog (an illustrated screenshot is provided below). In the text box, you may write cmd to invoke a window with black background graced with white text. This window is the command prompt or the command window. 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.

Java Programming Run dialog.png

To learn more about the Command Prompt syntax, take a look at this MS-DOS tutorial.

[edit] 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, type the following command in the command/terminal window.

java -version

Here is the output on a RedHat Fedora Core 4 installation.

$ java -version
Eclipse Java Compiler 0.548, pre-3.1.0 milestone-6, Copyright IBM Corp 2000, 2005. All rights reserved.

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.

If you get an error, such as:

javac: command not found

or alternatively on Windows:

Bad command or file name

..then the JDK may not be installed or it may not be in your path. You can use your system's file search utilities to see if there is a javac or 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.

[edit] 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 on the left-hand side as follows:

HKEY_LOCAL_MACHINE > SOFTWARE > JavaSoft > Java Development Kit

The resultant would be similar to the image below, 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.

Java Programming Regedit Av.png

NOTE:
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.

[edit] Downloading Java

[edit] Understanding your platform

If a JDK is not already on your system, you should download one. But first things first - you need to understand what platform you have. The JDK is available on the Sun website but requires your choice to platform to proceed. Be sure to select a JDK that matches not only your operating system but your hardware system. The downloads available on the above mentioned website include the following platforms: Windows, Linux, Solaris SPARC, Solaris x86 and x64, Linux x64 and Windows x64.

[edit] What to do if a download isn't available for your platform?

For other operating systems not listed in the downloads, the system vendor probably has a download of the JDK (sometimes called a Software Development Kit or SDK), or you may have to download a product which includes the JDK. For example for Linux running on the x86 architecture, you can download an RPM (RedHat Package Manager) file or a self extracting .bin binary (such as jdk-1_5_0_06-linux-i586.bin for Intel Pentium compatible machines) to get Java SE release 5. The most stable version of JDK available at the time of writing is version 5.0.

NOTE:
The file name changes as new versions of Java are released.

[edit] Betas and the Nightly Builds

The Java community online is continuously updating the Java Development Kit and all the fundamental tools needed alongside those builds. One needs to be aware of these specific milestones in the development of a JDK. The version most of the authors of this book are using is the 5.0 version (mostly because it is stable and secure). But, for a taste of the latest in the Java development process, you should visit the Java.Net JDK repository online to fetch the most recent betas and nightly builds.

Most of the projects undergoing for the development of Java Development Kits have nicknames that the developer circles use. As of the time of writing this particular line, Java.Net lists two pre-release versions of the JDK namely Mustang (JDK 6) and Dolphin (JDK 7). Be sure to check for the latest updates on the above mentioned repository (you will be surprised by just how often you get to see a new version).

[edit] Install the JDK

When you execute the installer, it will install the JDK files. Note: you will need administrator access to install the JDK. You can choose an installation location, such as c:\java\jdk\5.0 on Windows or /var/local/java/5.0 on Linux. or use the default. This location is called the JAVA_HOME and it is customary but not necessary to define an environment variable which refers to that location.

You may also have to modify your path so that it includes the bin subdirectory of your JAVA_HOME. You should put the JAVA_HOME/bin directory before other directories where Java is installed. The above installations may put the JRE, java (or java.exe) in a system bin directory, but they may not put all of the JDK executables in your path.

You may need to restart your command shell in order to pick up environment variable changes for JAVA_HOME and PATH.

[edit] Install an IDE

You may optionally choose to install an IDE. There are many free and commercial IDE's available. For example, the Eclipse IDE found at http://www.eclipse.org includes many of the tools necessary to build Java programs (such as a Java compiler, debugger, Javadoc, etc.) and thus you do not need to install a JDK if you opt to use Eclipse. However, Eclipse does require you to install a JRE - Eclipse is a Java program that runs within the JRE. The JDK download sites mentioned above also include JRE downloads, and installation instructions are similar.

Another Popular IDE for the Windows platform is Borland's JBuilder from http://www.borland.com

And a popular Free IDE is JCreator LE from XINOX Software found at http://www.jcreator.com

However, even if you choose to use Eclipse, it is recommended that you download and install the JDK. This will allow you to use scripts to build and test your Java software from outside the IDE environment.

If you download Java from Sun's site, you also have the option of installing the NetBeans IDE + JDK 5.0 Update 6 bundle.

[edit] Install Validation

Once you are content that the installations for the Java Development Kit and the Java Runtime Environment have reached completion, you may repeat the steps to check for the availability for Java as mentioned on the top of this section of the book. If, otherwise, you still reach negative conditions try working out through each step to pick where the mistake might have had occurred.

If the above text fails to address your needs, leave a suggestion on the Talk page.

[edit] External Links