Department of Computer Science
Faculty of Physical Sciences
Ahmadu Bello University, Zaria

COSC 211 : Object Oriented Programming I - LAB00

Contents

What is JDK

Java Development Kit (JDK), officially named "Java Platform Standard Edition (Java SE)", which is freely available from Sun Microsystems (now part of Oracle), is needed for writing Java programs. For more details go to http://www.oracle.com/technetwork/java/index.html. JRE (Java Runtime Environment) is also needed for running Java programs but once installed the JDK it comes with JRE.

How to Download JDK (Windows)

Step 1: Un-install Older Version(s) of JDK/JRE or Go to Step 1

It's recommended that you should install only the latest JDK. Although you can install multiple versions of JDK concurrently but is messy. If you have already installed older version(s) of JDK/JRE, un-install All of them.

Step 2: Download JDK (If already have it go to Installation guides)

  1. Go to Java SE download site http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. Under "Java Platform, Standard Edition"==>"Java SE 8u{xx}", where {xx} is the latest update of number ==> Click "Java Download" button.
  3. Read and check "Accept License Agreement".
  4. Choose your operaing system platform, e.g., "Windows x64 (for 64-bit Windows OS) or "Windows x86 (for 32-bit windows OS). You can check whether your windows OS is 32-bit or 64-bit via "Control Panel"==>"System"==>Under"System Type".

How to Install JDK 8 (on Windows)

Step 1: Install JDK and JRE

Run the downloaded installer (e.g., "jdk-8u{xx-windows-x64.exe}"), which install both the JDK and JRE, By Default, the JDK will be installed in directory "C:\Program Files\Java\jdk1.8.0_xx", where xx denotes the latest upgrade number; and JRE in "C:\Program Files\Java\jre1.8.0_xx".

For novices, accept the defaults, Follow the screen instruction to install the JDK and JRE

Check the JDK installed directory by inspecting these folders by going to "C: drive" ==> "Program Files" ==> "Java". Take note of your JDK installed directory, which you will need in the next step.

Step 2: Include JDK's "bin" Directory in the PATH

Windows OS searches the current directory and the directories listed in the PATH environment variable for executable programs. JDK's programs (such as Java compiler javac.exe and Java runtime java.exe) reside in directory "\bin" (where denotes the JDK installed directory). You need to include "\bin" in the PATH to run the JDK programs.

To edit the PATH environment variable in Windows XP/Vista/7/8/10:

  1. Launch "Control Panel" ==> "System" ==> Click "Advanced system settings".
  2. Switch to "Advanced" tab ==> "Environment Variables".
  3. Under "System Variables", scroll down to select "Path" ==> "Edit...".
  4. (CAUTION: Read this paragraph 3 times before doing this step! There is no UNDO)

    For Windows 10: You see a table listing the existing PATH entries. Click "New" ==> Enter the JDK's binary directory "c:\Program Files\Java\jdk1.8.0_xx\bin" (Replace xx with your installation's upgrade number!!!) ==> Select "Move Up" to move it all the way to the top.

    Prior to Windows 10: In "Variable value" field, INSERT "C:\Program Files\Java\jdk1.8.0_xx\bin" (Replace xx with your installation upgrade number!!!) IN FRONT of all the existing directories, followed by a semi-colon (;) which separates the JDK's binary directory from the rest of the existing directories. DO NOT DELETE any existing entries; otherwise, some existing applications may not run.

    Variable name : PATH
    Variable value : C:\Program Files\Java\jdk1.8.0_xx\bin; [exiting entries...]

Step 3: Verify the JDK Installation

Launch a CMD shell (Click "Start" button ==> run... ==> enter "cmd"; OR from "Start" button ==> All Programs ==> Accessories ==> Command Prompt).

  1. Issue "path" command to list the contents of the PATH environment variable. Check to make sure that your \bin is listed in the PATH.
    // Display the PATH entries
    prompt>path
    PATH=c:\Program Files\Java\jdk1.8.0_xx\bin;[other entries...]
    prompt>_

    Don't type prompt>, which denotes the command prompt!!! Key in the command (highlighted) only.

  2. Issue the following commands to verify that JDK/JRE are properly installed and display their version:
    Display the JRE version and the JDK version
    prompt> java -version
    java version "1.8.0_xx"
    Java(TM) SE Runtime Environment (build 1.8.0_xx-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
    prompt> javac -version
    javac 1.8.0_xx
    prompt>_

My First Program on Windows

Step 1: Write a Hello-World Java Program

  1. Create a directory to keep your works, e.g.,"C:\myProject", or any directory of your choice. The directory name shall not contain blank or special characters. Use meaningful but short name as it is easier to type.
  2. Launch a programming text editor (such as NotePad, TextPad or any text editor recommended by your lab instructor). Begin with a new file and enter the following source code. Save the file as "Hello.java", under your work directory (e.g., C:\myProject).
  3. /*
     * My first Java program to say Hello
     */
     // Save as "Hello.java" under "C:\myProject"
    public class Hello {
       public static void main(String[] args) {
    	  System.out.println("Hello, world!");
       }
    }
    

Step 2: Compile and Run the Hello-World Java Program

To compile the source code "Hello.java":

  1. Start a CMD Shell (Click the "Start" button ==> "run..." ==> Enter "cmd").
  2. Set the Current Drive to the drive where you saved your source file "Hello.java". For example, suppose that your source file is saved in drive "C", enter "C:" as follow:
    prompt>C:
    C:\Users\user-pc>_
    C:\Users\user-pc>cd ..
    C:\Users>cd ..
    C:\>_
    Don't enter prompt>, which denotes the command prompt.
  3. Set the Current Working Directory to the directory that you saved your source file via the cd (Change Directory) command. For example, suppose that your source file is saved in directory "C:\myProject".
    C:\>cd \myProject
    C:\myProject>_
  4. Invoke the JDK compiler "javac" to compile the source code "Hello.java".
    D:\myProject>javac Hello.java

    The compilation is successful if the command prompt returns. Otherwise, error messages would be shown. Correct the errors in your source file and re-compile. Check "Common JDK Installation Errors", if you encounter problem compiling your program.

  5. The output of the compilation is a Java class called "Hello.class". Issue a dir (List Directory) command again to check for the output.
    D:\myProject>dir
    ......
    xx-xxx-xx  01:53 PM               416 Hello.class
    xx-xxx-xx  06:25 PM               277 Hello.java
    ......

    To run the program, invoke the Java Runtime "java":

    D:\myProject>java Hello
    Hello, world!

How to Download JDK (Mac OS)

Step 1: Check if JDK has been Pre-Installed

In some Mac systems (earlier than Mac OS X 10.7 Lion), JDK has been pre-installed. To check if JDK has been installed, open a "Terminal" (Finder ==> Go ==> Utilities ==> Terminal) and issue this command:

javac -version

Step 2: Download JDK

  1. Goto Java SE download site @ http://www.oracle.com/technetwork/java/javase/downloads/index.html. Under "Java Platform, Standard Edition" ==> "Java SE 8u{xx}" ==> Click the "JDK Download" button
  2. Check "Accept License Agreement"
  3. Choose your operating platform, e.g., "Mac OS X" (jdk-8u{xx}-macosx-x64.dmg). Download the installer.

Install JDK/JRE (on Mac OS)

  1. Double-click the downloaded Disk Image (DMG) file. Follow the screen instructions to install JDK/JRE.
  2. Eject the DMG file.
  3. To verify your installation, open a "Terminal" and issue these commands.

    Display the JDK version

    javac -version
    javac 1.x.x_xx
    

    Display the JRE version

    java -version java version "1.x.x_xx" Java(TM) SE Runtime Environment (build 1.x.x_xx-xxx) Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)

    Display the location of Java Compiler

    which javac /usr/bin/javac

    Display the location of Java Runtime

    which java /usr/bin/java

My First Program on Windows

Step 1: Write a Hello-World Java Program

  1. Create a directory called "myProject" under your home directory (Launch "Finder" ==> "Go" ==> "Home"; Select "File" ==> "New Folder" ==> "myProject"). In Mac OS, the home directory of the current login user is denoted as "~". Hence, this new directory is represented as "~/myProject".
  2. Use a programming text editor (such as jEdit, gedit, Sublime Text or any text editor recommended by your lab instructor) to input the following source code and save as "Hello.java" under the directory "~/myProject". [If you use Mac OS's default text editor "TextEdit" (NOT recommended), you need to open a new file ==> choose "Format" ==> "Make Plain Text" ==> Enter the source code ==> Save as "Hello.java".]
/*
 * My First Java program to say Hello
 */
// Save as "Hello.java" under "~/myProject"
public class Hello {   
   public static void main(String[] args) {
      System.out.println("Hello, world!");
   }
}

Step 2: Compile and Run the Hello-World Java Program

  1. To compile the source code "Hello.java", open a new "Terminal" ("Go" ==> "Utilities" ==> "Terminal") and issue these commands (as illustrated):

    Change Directory (cd) to where "Hello.java" resides

    cd ~/myProject

    Check if "Hello.java" exists using list (ls) command

    ls
    Hello.java ......

    Compile "Hello.java" using JDK compiler "javac"

    javac Hello.java
    If error message appears, correct your source code and re-compile

    Check for the compiled output "Hello.class"

    ls
    Hello.class
    Hello.java ......
  2. To run the Hello-world, invoke the Java Runtime "java" as follows:

    Run "Hello.class"

    java Hello
    Hello, world!