Downloading and Installing JDK

2. How to Install JDK on Mac OS X

Step 1: Check if JDK has been Pre-Installed

To check if JDK has been installed, open a "Terminal" (Search "Terminal"; or Finder ⇒ Go ⇒ Utilities ⇒ Terminal) and issue this command:

javas - version
  • If a JDK version number is returned (e.g., JDK x.x.x), then JDK has already been installed. If the JDK version is prior to 1.8, proceed to Step 2 to install the latest JDK; otherwise, proceed to "Step 3: Write a Hello-world Java program".
  • If message "command not found" appears, JDK is NOT installed. Proceed to the "Step 2: Install JDK".
  • If message "To open javac, you need a Java runtime" appears, select "Install" and follow the instructions to install JDK. Then, proceed to "Step 3: Write a Hello-world Java program".

Step 2: Download JDK

  1. Goto Java SE download site @ http://www.oracle.com/technetwork/java/javase/downloads/index.html .
  2. Under "Java Platform, Standard Edition" ⇒ "Java SE 11.0.{x}", where {x} denotes a fast running security-update number ⇒ Click the "Oracle JDK" "Download" button.
  3. Under "Java SE Development Kit 11.0.{x}" ⇒ Check "Accept License Agreement".
  4. Choose the JDK for your operating platform, i.e., MacOS. Download the DMG installer (e.g, jdk-11.0.{x}_osx-x64_bin.dmg  - about 166MB).

Step 3: Install JDK/JRE

  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 11.0.{x}
     
    // Display the JRE version
    java -version
    java version "11.0.{x}"
    ......
     
    // Display the location of Java Compiler
    which javac
    /usr/bin/javac
     
    // Display the location of Java Runtime
    which java
    /usr/bin/java           

Step 3: 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 X, the home directory of the current user can be referenced as "~". Hence, this new directory can be referenced as "~/myProject".
  2. Use a programming text editor (such as Sublime Text or Atom) to input the following source code and save as "Hello.java" under the directory "~/myProject".
    (If you use Mac OS X'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
     */
    public class Hello {   // Save as "Hello.java" under "~/myProject"
       public static void main(String[] args) {
          System.out.println("Hello, world from Mac!");
       }
    }   

Step 4: 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):
  2. // 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   ......
  1. To run the Hello-world, invoke the Java Runtime "java" as follows:
    
        
    java Hello
    Hello, world from Mac!