Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mohanpedala/700910ee504ed7f12bdf0249ca7d11e2 to your computer and use it in GitHub Desktop.
Save mohanpedala/700910ee504ed7f12bdf0249ca7d11e2 to your computer and use it in GitHub Desktop.
Java SE Development Kit Installation on Mac and Windows

macOS

Install the binary files provided by Oracle

  • Download JDK 8 from Oracle WebSite. [jdk-8u65-macosx-x64.dmg for Mac].
  • Double click on jdk-8u65-macosx-x64.dmg and follow the screen instructions.
  • Once the JDK package is installed check the below output
$ ls -l /Library/Java/JavaVirtualMachines
total 0
drwxr-xr-x  3 root  wheel  96 Mar  5 09:53 jdk1.8.0_201.jdk
  • Verify JDK version
$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
  • Set the JAVA_HOME in your .bash_profile or .zshrc(If you are using Zshell)
######## JAVA_HOME PATH ##########

export JAVA_HOME=/Library/Java/Home

####### END of JAVA_HOME PATH ########
  • If the above path creates an issue while using maven, please replace the JAVA_HOME with the below path
# Maven error
$ mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
$ vim ~/.zshrc 

export JAVA_HOME=$(/usr/libexec/java_home)

$ source ~/.zshrc

=======================================================================================

Windows 10 OS

Jdk installation

  • Check if any older version exists on the machine. If exists uninstall it.
  • Goto Java SE download site @ http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  • Run the downloaded installer (e.g., "jdk-8u{xx}-windows-x64.exe"), which installs 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 upgrade number; and JRE in "C:\Program Files\Java\jre1.8.0_xx".
Configure environment variables
To edit the PATH environment variable in Windows 7/8/10:

1. Launch "Control Panel" ⇒ (Optional) System and Security ⇒ System ⇒ Click "Advanced system settings" on the left pane.
2. Switch to "Advanced" tab ⇒ Push "Environment Variables" button.
3. Under "System Variables" (the bottom pane), scroll down to select "Path" ⇒ Click "Edit...".

For Windows 10 (newer releases):
You shall see a TABLE listing all the existing PATH entries (if not, goto next step). Click "New" ⇒ Enter the JDK's binary directory "c:\Program Files\Java\jdk1.8.0_xx\bin" (Replace xx with your installation number!!!) ⇒ Select "Move Up" to move this entry all the way to the TOP.

Prior to Windows 10:
(CAUTION: Read this paragraph 3 times before doing this step! Don't push "Apply" or "OK" until you are 101% sure. There is no UNDO!!!)
(To be SAFE, copy the content of the "Variable value" to Notepad before changing it!!!)

In "Variable value" field, INSERT "c:\Program Files\Java\jdk1.8.0_xx\bin" (Replace xx with your installation 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...]
Verify JDK installation
  • Open command prompt (Start > in serach bar > type cmd.exe)
  1. Issue "path" command to list the contents of the PATH environment variable. Check to make sure that your <JAVA_HOME>\bin is listed in the PATH. Don't type prompt>, which denotes the command prompt!!! Key in the command (highlighted) only.
// Display the PATH entries
prompt> path
PATH=c:\Program Files\Java\jdk1.8.0_xx\bin;[other entries...]
  1. Issue the following commands to verify that JDK/JRE are properly installed and display their version:
// Display the JRE 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)
 
// Display the JDK version
prompt> javac -version
javac 1.8.0_xx
Write hello world program
  1. Create a directory to keep your works, e.g., "d:\myProject", or "c:\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++, or Sublime Text, or Atom). 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!");
   }
}
Compile and Run hello world 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 "d", enter "d:" as follow:
  3. Navigate to d:\myProject folder
  4. Type dir
D:\myProject> dir
......
xx-xxx-xx  06:25 PM               277 Hello.java
......
  1. Invoke the JDK compiler "javac" to compile the source code "Hello.java".
D:\myProject> javac Hello.java
  1. 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
......
  1. To run the program, invoke the Java Runtime "java":
D:\myProject> java Hello
Hello, world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment