14 Oct 2013

How to install Java in Windows and Mac Operating system






Java Development Kit (JDK) 1.7 (officially named Java SE 7), which is freely available from Sun Microsystems (now part of Oracle), is needed for writing Java programs. JDK can be downloaded from the Java mother site @ http://java.sun.com (or http://www.oracle.com/technetwork/java/index.html).
JDK or JRE?
JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit), which includes JRE plus the development tools (such as compiler and debugger), is need for writing as well as running Java programs. Since you are supposed to write Java Programs, you should install JDK, which includes JRE.

Step 0(a): Un-Install Older Version(s) of JDK/JRE
I recommend that you install the latest JDK, but it can be messy if you have multiple versions of JDK/JRE.
If you have previously installed older version(s) of JDK/JRE, un-install ALL of them. Run "Control Panel" ⇒ Program and Features ⇒ Un-install programs begin with "Java", such as "Java SE Development Kit" and "Java SE Runtime". If you are not sure whether you have older version(s) of JDK, check!
Step 0(b): Understand Windows' CMD Shell
Programmers need to know how to use CMD shell to issue commands. If you are completely new to CMD, read "Programmer's Survival Guide for Windows".
Step 1: Download JDK
  1. Goto Java SE download site @ http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  2. Click the "Download" button under "JDK" of "Java SE 7".
  3. Choose your operating platform, e.g., Windows x86 for 32-bit Windows OS or Windows x64 for 64-bit Windows OS. You can check whether your Windows OS is 32-bit or 64-bit via "Control Panel" ⇒ System ⇒ Under the "System Type".
Step 2: Install JDK and JRE
Run the downloaded installer (e.g., "jdk-7uxx-windows-i586.exe"), which installs both the JDK (Java Development Kit) and JRE (Java Runtime). By default, the JDK will be installed in directory "C:\Program Files\Java\jdk1.7.0_{xx}", where {xx} denotes the latest upgrade number; and JRE in "C:\Program Files\Java\jre7".
For novices, accept the defaults. Simply click "next"..."next"... to install JDK in "C:\Program Files\Java\jdk1.7.0_{xx}" and JRE in "C:\Program Files\Java\jre7". Take note of you JDK installed directory. I shall refer to the JDK installed directory as <JAVA_HOME>, hereafter, in this article.
(For Advanced Users Only) The default JDK/JRE directories work but I recommend avoiding "Program Files" directory because of that blank character in the directory name. You may change the installed directories for JDK and JRE during installation. I personally installed JDK and all my programming tools in "d:\bin" (instead of "C:\Program Files") for ease of maintenance.
Step 3: 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 "<JAVA_HOME>\bin" (where <JAVA_HOME> denotes the JDK installed directory, e.g., C:\Program Files\Java\jdk1.7.0_{xx}). You need to include the "<JAVA_HOME>\bin" directory in the PATH.
To edit the PATH environment variable in Windows 2000/XP/Vista/7/8:
  1. Click "Start" button ⇒ "Control Panel" ⇒ "System" ⇒ (Vista/7/8 only) "Advanced system settings".
  2. Switch to "Advanced" tab ⇒ "Environment Variables..."
  3. In "System Variables" box, scroll down to select "PATH" ⇒ "Edit..."
  4. (CAUTION: Read this paragraph 3 times before doing this step! There is no UNDO) In "Variable value" field, INSERT "c:\Program Files\Java\jdk1.7.0_{xx}\bin" (VERIFY that this is your JDK's binary directory) 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.7.0_{xx}\bin;[exiting entries]
(For Advanced Users Only)
I suggested that you place the JDK bin directory in front of "c:\windows\system32" and "c:\windows". This is because some Windows systems may have an out-dated copy of JDK/JRE in these directories. Do a search for "java.exe", and you will be amazed by the findings.
You could read "Java Applications and Environment Variable" for more discussions about PATH environment variable.
I also recommend that you define an environment variable called JAVA_HOME, which contains the JDK installed directory, and include the JDK bin directory in the PATH via JAVA_HOME, i.e., PATH=%JAVA_HOME%\bin;.....
Step 4: Verify the JDK Installation
Launch a CMD shell (Click "Start" button ⇒ run... ⇒ enter "cmd"; or "Start" button ⇒ All Programs ⇒ Accessories ⇒ Command Prompt).
  1. Issue a "path" command to list the contents of the PATH environment variable. Check the output and make sure that <JAVA_HOME>\bin is listed in the PATH.
    prompt> path
    PATH=c:\Program Files\Java\jdk1.7.0_{xx}\bin;[other entries]
  2. Issue the following commands to verify that JDK/JRE are properly installed and display their version:
    prompt> java -version
    java version "1.7.0_{xx}"
    Java(TM) SE Runtime Environment (build 1.7.0_{xx}-b11)
    Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
     
    prompt> javac -version
    javac 1.7.0_{xx}
Step 5: Write a Hello-World Java Program
  1. Create a directory to keep all your works, e.g., d:\myproject, or any directory of your choice. Do NOT save your works in "Desktop" or "Documents" as they are hard to locate. 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 TextPad or NotePad++). Begin with a new file and enter the following source code. Save the file as "Hello.java", under your work directory (e.g., d:\myproject).
    /*
     * First Java program to say Hello
     */
    public class Hello {   // Save as "Hello.java" under "d:\myproject"
       public static void main(String[] args) {
          System.out.println("Hello, world!");
       }
    }
Step 6: Compile and Run the Hello-World Java Program
JavaBasics_GettingStarted.png
  1. To compile the source code "Hello.java":
    1. Start a CMD Shell (Click "Start" button ⇒ Select "run..." ⇒ Enter "cmd"; or "Start" button ⇒ All Programs ⇒ Accessories ⇒ Command Prompt).
    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 "d", enter "d:" as follow:
      prompt> d:
      D:\xxx>
    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 "d:\myproject".
      D:\xxx> cd \myproject
      D:\myproject>
    4. Issue a dir (List Directory) command to confirm that your source file is present in the current directory.
      D:\myproject> dir
      ......
      08-May-XX  06:25 PM               277 Hello.java
      ......
    5. 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.
    6. 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
      ......
  2. To run the program, invoke the Java Runtime "java":
    D:\myproject> java Hello
    Hello, world!


Step 7: (Optional) Download JDK API Documentation, Samples and Demos
The JDK download does not include the documentation, which needs to be downloaded separately. In the past, I always insist that my students should have a local copy of JDK API Documentation. But, today, you can easily access the online copy by googling "JDK 7 Documentation".
To install JDK API documentation:
  1. From the Java SE download page (@ http://www.oracle.com/technetwork/java/javase/downloads/index.html), look for "Java SE 7 Documentation" (under "Additional Resources") ⇒ Download the zip-file (e.g., "jdk-7....zip" - about 57MB).
  2. Unzip into the <JAVA_HOME> (JDK installed directory). The documentation will be unzipped into "<$JAVA_HOME>\docs". Browse the JDK documentation by opening "<JAVA_HOME>\docs\index.html".
You should also download the "JDK Samples and Demos" from the Java SE download site.
Step 8: (For Advanced Users Only) JDK's Source Code
Source code for JDK is provided and kept in "<JAVA_HOME>\src.zip". I strongly recommend that you to go through some of the source files such as "String.java", "Math.java", and "Integer.java", under "java\lang".

1.1  Using TextPad for Java Programming (on Windows)

TextpadJava.png TextPad (@ www.textpad.com) is an excellent programming text editor for writing toy Java programs. It can be configured to couple with the JDK, hence, bypassing the CMD shell. From the TextPad editor, you can invoke the JDK compiler/runtime directly via "Tools" menu ⇒ External Tools ⇒ "Compile Java" or "Run Java Application". Take note of the keyboard shortcuts - Ctrl+1 for compile and Ctrl+2 for run.
If you cannot find these commands in the "Tools" menu, goto "Configure" ⇒ Preferences... ⇒ Tools ⇒ Add ⇒ JDK Commands. You can also configure the properties of "compile" and "run" there, such as prompting for command-line arguments.

1.2  Using NotePad++ for Java Programming (on Windows)

Notepad++ (@ http://notepad-plus-plus.org) is a free and open-source programming editor.
You can use NotePad++ to input Java source code, and compile and run the Java program under CMD shell.
NotePad++ Customization and Tips
You can customize Notepad++ to compile and run Java programs with hot-keys.  

No comments:

Post a Comment