How To Install JDK 8 (On Windows, Mac OS, Ubuntu)
How To Install JDK 8 (On Windows, Mac OS, Ubuntu)
The Java Development Kit (JDK), officially named "Java Platform, Standard Edition (Java SE)" is needed for writing Java programs.
The JDK is freely available from Sun Microsystems (now part of Oracle). The mother site for JDK (Java SE)
is http://www.oracle.com/technetwork/java/javase/overview/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 (developing) as well as running Java programs. In other words, JRE is a
subset of JDK. Since you are supposed to write Java Programs, you should install JDK, which includes JRE.
JDK Versions
The various JDK versions are:
1. JDK Alpha and Beta (1995): Sun announced Java in September 23, 1995.
2. JDK 1.0 (January 23, 1996): Originally called Oak (named after the oak tree outside James Gosling's office). Renamed to
Java 1 in JDK 1.0.2.
3. JDK 1.1 (February 19, 1997): Introduced AWT event model, inner class, JavaBean, JDBC, and RMI.
4. J2SE 1.2 (JDK 1.2) (December 8, 1998): Re-branded as "Java 2" and renamed JDK to J2SE (Java 2 Standard Edition). Also
released J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro Edition). Included JFC (Java Foundation Classes - Swing,
Accessibility API, Java 2D, Pluggable Look and Feel and Drag and Drop). Introduced Collection Framework and JIT compiler.
5. J2SE 1.3 (JDK 1.3) (May 8, 2000): Introduced Hotspot JVM.
6. J2SE 1.4 (JDK 1.4) (February 6, 2002): Introduced assert, non-blocking IO (nio), logging API, image IO, Java webstart,
regular expression support.
7. J2SE 5.0 (JDK 1.5) (September 30, 2004): Officially called 5.0 instead of 1.5. Introduced generics, autoboxing/unboxing,
annotation, enum, varargs, for-each loop, static import.
8. Java SE 6 (JDK 1.6) (December 11, 2006): Renamed J2SE to Java SE (Java Standard Edition).
9. Java SE 7 (JDK 1.7) (July 28, 2011): First version after Oracle purchased Sun (called Oracle JDK).
10. Java SE 8 (JDK 1.8) (March 18, 2014): included support for Lambda expressions, default and static methods in interfaces,
improved collection, and JavaScript runtime. Also integrated JavaFX graphics subsystem.
11. Java SE 9 (JDK 9) (September 21, 2017): introduced modularization of the JDK (module) under project Jigsaw, the Java
Shell (jshell), and more.
12. Java SE 10 (JDK 10) (March, 2018): introduced var for type inference local variable (similar to JavaScript). There will be 2
releases each year, in March and September.
Use the "File Explorer", goto "C:\Program Files\Java" to inspect these folders. Take note of your JDK installed directory, in
particular, the varying upgrade number, which you will need in the next step.
In the following diagram, the JDK installed directory is "C:\Program Files\Java\jdk-10.0.1", where {x}=1. I shall refer to
the JDK installed directory as <JAVA_HOME>, hereafter, in this article.
Notes: Starting from JDK 1.8, the installation created a directory "c:\ProgramData\Oracle\Java\javapath" and added to the
PATH. It contains only JRE executables (java.exe, javaw.exe, and javaws.exe), but NOT the JDK executables (e.g., javac.exe).
2. Issue the following commands to verify that JDK/JRE are properly installed and display their version:
/*
* 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!");
}
}
prompt> d:
D:\xxx>
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
"d:\myProject".
D:\xxx> cd \myProject
D:\myProject>
D:\myProject> dir
......
xx-xxx-xx 06:25 PM 277 Hello.java
......
D:\myProject> dir
......
xx-xxx-xx 01:53 PM 416 Hello.class
xx-xxx-xx 06:25 PM 277 Hello.java
......
Ever ything that can possibly go wrong will go wrong : Read "JDK Installation Common Errors".
TextPad (@ www.textpad.com) is a lightweight 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.
SET JAVA_HOME
If you get a message "Environment variable JAVA_HOME not defined", proceed to the next step.
If you get "JAVA_HOME=C:\Program Files\Java\jdk-10.0.{x}", verify that it is set correctly to your JDK directory. If not,
proceed to the next step.
3. To set the environment variable JAVA_HOME in Windows 10/8/7: Launch "Control Panel" ⇒ (Optional) System and Security ⇒
System ⇒ Advanced system settings ⇒ Switch to "Advanced" tab ⇒ Environment Variables ⇒ System Variables (the bottom
pane) ⇒ "New" (or look for "JAVA_HOME" and "Edit" if it is already set) ⇒ In "Variable Name", enter "JAVA_HOME" ⇒ In
"Variable Value", enter your JDK installed directory you noted in Step 1. (In the latest Windows 10: you can push the "Browse
Directory..." button and select the JDK installed directory to avoid typo error.)
4. To verify, RE-START a CMD (restart needed to refresh the environment) and issue:
SET JAVA_HOME
JAVA_HOME=c:\Program Files\Java\jdk-10.0.{x} <== Verify that this is YOUR JDK installed directory
javac -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".
/*
* 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!");
}
}
// Run "Hello.class"
java Hello
Hello, world from Mac!
$ javac -version
If a JDK version number (e.g., "javac x.x.x") appears, JDK has already been installed. You can skip the installation and goto step
2.
To remove OpenJDK, issue command:
$ cd /usr/local
$ sudo mkdir java
$ cd /usr/local/java
$ sudo tar xzvf ~/Downloads/jdk-10.0.{x}-linux-x64_bin.tar.gz
// x: extract, z: for unzipping gz, v: verbose, f: filename
The above steps set up symlinks java, javac, javaws at /usr/bin(which is in the PATH), that link
to /etc/alternatives and then to JDK bin directory.
The "alternatives" system aims to resolve the situation where several programs fulfilling the same function (e.g., different
version of JDKs). It sets up symlinks thru /etc/alternatives to refer to the actual programs to be used.
$ ls -ld /usr/bin/java*
lrwxrwxrwx 1 root root 22 Mar 31 20:41 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 23 Mar 31 20:42 /usr/bin/javac -> /etc/alternatives/javac
lrwxrwxrwx 1 root root 24 Mar 31 20:42 /usr/bin/javaws -> /etc/alternatives/javaws
$ ls -ld /etc/alternatives/java*
lrwxrwxrwx 1 root root 40 Aug 29 18:18 /etc/alternatives/java -> /usr/local/java/jdk-10.0.{x}/bin/java
lrwxrwxrwx 1 root root 37 Aug 29 18:18 /etc/alternatives/javac -> /usr/local/java/jdk-10.0.{x}/bin/javac
lrwxrwxrwx 1 root root 42 Aug 29 18:19 /etc/alternatives/javaws -> /usr/local/java/jdk-10.0.{x}/bin/javaws
Alternatively, you can include the JDK's bin and JRE's bin into the PATH directly.
4. To verify the JDK installation, issue these commands:
$ which java
/usr/bin/java
5. (Optional) To use Java under Firefox, you need to enable the so-called "Java Plugin for web browser".
$ cd ~/.mozilla/plugins
// if this directory does not exist, create it
$ mkdir ~/.mozilla/plugins
Then, create a symbolic link to your Mozilla plugins folder, (check your JDK folder)
$ cd ~/.mozilla/plugins
$ sudo ln -s /usr/local/java/jdk-10.0.{x}/jre/lib/amd64/libnpjp2.so
To verify the installation, restart your Firefox and issue URL "about:plugins". Check for Java plugins with the correct
version.
Starting from JDK 1.8, to run unsigned applets, you need to set security level to "high" add the sites to the "Exception List"
(under the Java Control Panel ⇒ Security). To start the Java Control Panel:
$ cd /usr/local/java/jdk-10.0.{x}/jre/bin
$ ./ControlPanel // OR ./jcontrol
$ cd /etc
$ gksudo gedit profile // OR "sudo nano profile" to use the console-based nano editor
Add these lines at the end of the file "/etc/profile", replace "{x}" with the actual number:
export JAVA_HOME=/usr/local/java/jdk-10.0.{x}
export PATH=$JAVA_HOME/bin:$PATH
// Refresh
$ source /etc/profile
$ echo $PATH
.....:/usr/local/java/jdk-10.0.{x}/bin
3. To compile the Hello-world Java program, launch a Terminal and issue these commands:
// Run "Hello.class"
$ java Hello
Hello, world from Ubuntu!
4. First Java Program with Eclipse
1. You need to first install Eclipse. Read "How to Install Eclipse".
2. You can then proceed to write your first Java program. Read "Writing your first Java Program with Eclipse".
3. Eclipse allow you to debug program graphically. Read "Debugging program in Eclipse".
export CLASSPATH=.:path1/xxx.jar:path2/yyy.jar
// For Windows
// Compile Java source code
javac -cp .;path1\xxx.jar;path2\yyy.jar ClassName.java
// Run Java class
java -cp .;path1\xxx.jar;path2\yyy.jar ClassName
// For Mac OS X and Ubuntu
// Compile Java source code
javac -cp .:path1/xxx.jar:path2/yyy.jar ClassName.java
// Run Java class
java -cp .:path1/xxx.jar:path2/yyy.jar ClassName