How to Install JDK 10 (on Windows, Mac OS & Ubuntu) and Get Started with Java Programming


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

"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.

How To Install JDK on Windows

Step 0: Un-Install Older Version(s) of JDK/JRE

I recommend that you install only the latest JDK. Although you can install multiple versions of JDK/JRE concurrently, it is messy.

If you have previously installed older version(s) of JDK/JRE, un-install ALL of them. Goto "Control Panel" ⇒ "Programs" ⇒ "Programs and Features" ⇒ Un-install ALL programs begin with "Java", such as "Java SE Development Kit ...", "Java SE Runtime ...", "Java X Update ...", and etc.

Step 1: Download JDK
  1. Goto Java SE download site @
  2. Under "Java Platform, Standard Edition" ⇒ "Java SE 10.0.{x}", where {x} denotes a fast running update number ⇒ Click the JDK's "Download" button.
  3. Under "Java SE Development Kit 10.0.{x}" ⇒ Check "Accept License Agreement".
  4. Choose the JDK for your operating system, i.e., "Windows" (for 64-bit Windows OS), and download the installer (e.g., "jdk-10.0.{x}_windows-x64_bin.exe" - 390MB).
Step 2: Install JDK and JRE

Run the downloaded installer (e.g., "jdk-10.0.{x}_windows-x64_bin.exe"), which installs both the JDK and JRE.

By default:

  • JDK is installed in directory "C:\Program Files\Java\jdk-10.0.{x}", where {x} denotes the upgrade number; and
  • JRE is installed in "C:\Program Files\Java\jre-10.0.{x}".

Notes: In 64-bit Windows, "Program Files" is meant for 64-bit programs; while "Program Files (x86)" for 32-bit programs.

Accept the defaults and follow the screen instructions to install JDK and JRE.

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.

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

Windows' Shell searches the current directory and the directories listed in the PATH environment variable (system variable) for executable programs. JDK's programs (such as Java compiler javac.exe and Java runtime java.exe) reside in the sub-directory "bin" of 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 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...".
  4. 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 "bin" directory "c:\Program Files\Java\jdk-10.0.{x}\bin" (Replace {x} with your installation number!!!) ⇒ Select "Move Up" to move this entry all the way to the TOP.

    Prior to newer 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\jdk-10.0.{x}\bin" (Replace {x} with your installation number!!!) IN FRONT of all the existing directories, followed by a semi-colon (;) which separates the JDK's bin 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\jdk-10.0.{x}\bin;[do not delete exiting entries...]

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).

Step 4: Verify the JDK Installation

Launch a CMD shell via one of the following means:

  1. Click "Search" button ⇒ Enter "cmd" ⇒ Choose "Command Prompt", or
  2. right-click "Start" button ⇒ run... ⇒ enter "cmd", or
  3. (Prior to Windows 10) click "Start" button ⇒ All Programs ⇒ Accessories (or Windows System) ⇒ Command Prompt, or
  4. (Windows 10) click "Start" button ⇒ Windows System ⇒ Command Prompt.

Issue the following commands to verify your JDK installation:

  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\jdk-10.0.{x}\bin;[other entries...]
  2. 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 "10.0.{x}" 2018-04-17
    Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
    Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
    // Display the JDK version
    prompt> javac -version
    javac 10.0.{x}
Step 5: Write a Hello-World Java 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 "", under your work directory (e.g., d:\myProject).
     * First Java program to say Hello
    public class Hello {   // Save as "" under "d:\myProject"
       public static void main(String[] args) {
          System.out.println("Hello, world!");
Step 6: Compile and Run the Hello-World Java Program

To compile the source code "":

  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 "". For example, suppose that your source file is saved in drive "d", enter "d:" as follow:
    prompt> d:
    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
  4. Issue a dir (List Directory) command to confirm that your source file is present in the current directory.
    D:\myProject> dir
    xx-xxx-xx  06:25 PM               277
  5. Invoke the JDK compiler "javac" to compile the source code "".
    D:\myProject> javac
    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

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

D:\myProject> java Hello
Hello, world!

Everything that can possibly go wrong will go wrong: Read "JDK Installation Common Errors".

Step 7: (Optional) Download JDK API Documentation, Demos and Samples

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 10 Documentation".

To install JDK API documentation:

  1. From the Java SE download page (@, under "Additional Resources", look for "Java SE 10 Documentation" ⇒ Download ⇒ Accept the license agreement ⇒ Download the zip-file (e.g., "jdk-10.0.{x}" - about 66MB).
  2. Unzip into the JDK installed directory <JAVA_HOME>. 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 x 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>\lib\" (or "<JAVA_HOME>\" prior to JDK 9). I strongly recommend that you to go through some of the source files such as "", "", and "", under "java\lang".

Using TextPad for Java Programming (on Windows)


TextPad (@ 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.

TextPad Tips and Configuration

Check HERE!

Using NotePad++ for Java Programming (on Windows)

Notepad++ (@ is a free and open-source programming editor.

You can use NotePad++ to write your Java source code. But you need to use CMD shell to compile and run the Java program.

NotePad++ Customization and Tips

You can customize Notepad++ to compile and run Java programs with hot-keys. Read "NotePad++".

How To Set the Environment Variable JAVA_HOME

Many Java applications (such as Tomcat) require the environment variable JAVA_HOME to be set to the JDK installed directory.

To set the JAVA_HOME environment variable:

  1. First, find your JDK installed directory. For JDK 10, the default is "c:\Program Files\Java\jdk-10.0.xx", where xx is the upgrade number. Use your "File Explorer" to view this directory and take note of your JDK installed directory.
  2. Check if JAVA_HOME is already set. Start a CMD and issue:
    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:
    JAVA_HOME=c:\Program Files\Java\jdk-10.0.{x}   <== Verify that this is YOUR JDK installed directory

Notes: Windows' environment variables (such as JAVA_HOME, PATH) are NOT case-sensitive.

How to Install JDK on Mac OS X

Step 1: Check if JDK has been Pre-Installed

In some Mac systems (earlier than Mac OS X 10.7), 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:

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".
Step 2: Download JDK
  1. Goto Java SE download site @ Under "Java Platform, Standard Edition" ⇒ "Java SE 10.0.{x}" ⇒ Click the JDK's "Download" button.
  2. Check "Accept License Agreement".
  3. Choose your operating platform, i.e., MacOS. Download the installer (e.g, jdk-10.0.{x}_osx-x64_bin.dmg - 395MB).
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 10.0.{x}
    // Display the JRE version
    java -version
    java version "10.0.{x}"
    // Display the location of Java Compiler
    which javac
    // Display the location of Java Runtime
    which 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 jEdit, gedit, Sublime Text, or Atom) to input the following source code and save as "" 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 "".]
     * My First Java program to say Hello
    public class Hello {   // Save as "" 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 "", open a new "Terminal" ("Go" ⇒ "Utilities" ⇒ "Terminal") and issue these commands (as illustrated):
    // Change Directory (cd) to where "" resides
    cd ~/myProject
    // Check if "" exists using list (ls) command
    ls   ......
    // Compile "" using JDK compiler "javac"
    // If error message appears, correct your source code and re-compile
    // Check for the compiled output "Hello.class"
    Hello.class   ......
  2. To run the Hello-world, invoke the Java Runtime "java" as follows:
    // Run "Hello.class"
    java Hello
    Hello, world from Mac!

How to Install JDK on Ubuntu

There are several JDK implementations available for Linux, such as Oracle JDK, OpenJDK, Sun JDK, IBM JDK and GNU Java Compiler. We shall choose the Oracle JDK 8. Ubuntu chooses OpenJDK as its default JDK, which is not 100% compatible with Oracle JDK.

Step 0: Check if JDK has already been Installed

Open a Terminal and issue this command:

$ 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:

$ sudo apt-get purge openjdk-\*
Step 1: Download and Install JDK
  1. Goto JDK (Java SE) download site @ Under "Java Platform, Standard Edition" ⇒ "Java SE 10.0.{x}" ⇒ Click JDK's "Download" ⇒ Under "Java SE Development Kit 10.0.{x}" ⇒ Check "Accept License Agreement" ⇒ Select "Linux" (for 64-bit system) "tar.gz" package, (e.g., "jdk-10.0.{x}-linux-x64_bin.tar.gz" - 338MB). (To check your OS version, goto "Settings" ⇒ "Details".)
    The tarball will be downloaded in directory "~/Downloads", by default.
  2. We shall install JDK under "/usr/local/java" (or Ubuntu's default JDK directory /usr/lib/jvm; or /opt/java). First, create a directory "java" under "/usr/local". Open a Terminal and issue these commands:
    $ cd /usr/local
    $ sudo mkdir java
    Extract the downloaded package (Check your downloaded filename!)
    $ 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
    JDK shall be extracted in a folder "/usr/local/java/jdk-10.0.{x}", where {x} is the upgrade number.
  3. Inform the Ubuntu to use this JDK/JRE:
    // Setup the location of java, javac and javaws
    $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk-10.0.{x}/bin/java" 1
          // --install symlink name path priority
    $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk-10.0.{x}/bin/javac" 1
    $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk-10.0.{x}/bin/javaws" 1
    // Use this Oracle JDK/JRE as the default
    $ sudo update-alternatives --set java /usr/local/java/jdk-10.0.{x}/bin/java
          // --set name path
    $ sudo update-alternatives --set javac /usr/local/java/jdk-10.0.{x}/bin/javac
    $ sudo update-alternatives --set javaws /usr/local/java/jdk-10.0.{x}/bin/javaws
    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:
    // Show the Java Compiler (javac) version
    $ javac -version
    javac 10.0.{x}
    // Show the Java Runtime (java) version
    $ java -version
    java version "10.0.{x}"
    // Show the location of javac and java
    $ which javac
    $ which 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/
    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
    You need to restart Firefox after modifying the Exception List.
  6. [Don't Do this step - It is taken care by "alternative" in Step 3. Keep here to show you how to set PATH.]
    Add JDK's binary directory ("bin") to the "PATH" by editing "/etc/profile":
    $ 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
    Rerun the configuration file by:
    // Refresh
    $ source /etc/profile
    // Check the new settings for JAVA_HOME and PATH
    $ echo $JAVA_HOME
    $ echo $PATH
Step 2: Compile and Run a Hello-world Java Program
  1. File Explorer ⇒ Home ⇒ Create a new folder called "myProject" to keep our works.
  2. Open "Text Editor" (gedit). Enter the following source code and save as "" under the "~/myProject" directory created earlier.
    public class Hello {   // To save as "" under "~/myProject"
       public static void main(String[] args) {
          System.out.println("Hello, world from Ubuntu!");
  3. To compile the Hello-world Java program, launch a Terminal and issue these commands:
    // Change directory to where the source code resides
    $ cd ~/myProject
    // List the contents of current directory. Check for ""
    $ ls
    ...... ......
    // Compile "" into "Hello.class"
    $ javac
    // Check for "Hello.class"
    $ ls
    ...... Hello.class ......
  4. Run the Hello-world Java program:
    // Run "Hello.class"
    $ java Hello
    Hello, world from Ubuntu!

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".

First Java Program with NetBeans

  1. You need to first install NetBeans. Read "How to Install NetBeans".
  2. You can then proceed to write your first Java program. Read "Writing your first Java program with NetBeans".
  3. NetBeans allow you to debug program graphically. Read "Debugging program in NetBeans".

(Advanced) External JAR Files and Native Libraries

Notes: This section is applicable to JDK prior to JDK 9. JDK 9 introduces a new level called "module" on top of package, and "jmod" files for Java modules. Need to revise this section for JDK 9.

External Java packages (such as Servlet, MySQL Connector/J, JOGL, JUnit) are often distributed in JAR files (Java Archive - a single-file package of many Java classes), with possibly Native Libraries (".lib" and ".dll" in Windows, or ".a" and ".so" in Linux/Mac).

External JAR Files (".jar")

If external JAR files are not properly included:

  • During the compilation, you will receive compilation error "cannot find symbol" on classes belonging to the external packages.
  • During execution, you will get a runtime error "Could not find or load main class xxx" or "NoClassDefFoundError".

To include external JAR files, you can either:

  1. Copy all the JAR files of the external packages to the Java's Extension Directories (NOT applicable to JDK 9).
    • For Windows, the JDK extension directory is located at "<JAVA_HOME>\jre\lib\ext" (e.g., "c:\Program Files\Java\jdk1.8.0_xx\jre\lib\ext").
    • For Mac, the JDK extension directories are "/Library/Java/Extensions" and "/System/Library/Java/Extensions".
    • For Ubuntu, the JDK extension directories are "<JAVA_HOME>/jre/lib/ext" (e.g., "/usr/user/java/jdk1.8.0_xx/jre/lib/ext") and "/usr/java/packages/lib/ext".
    The location of JDK's extension directories is kept in Java's System Property "java.ext.dirs". You can print its contents via System.out.println(System.getProperty("java.ext.dirs")).
  2. You can also include all the JAR files in the CLASSPATH environment variable. CLASSPATH may contain directories (of Java classes) or JAR files (single-file archive of Java classes). If you set the CLASSPATH, you must also include the current directory (denoted as ".").
    • For Windows, set the CLASSPATH in Control Panel ⇒ System ⇒ Advanced system settings ⇒ Advanced ⇒ Environment Variables ⇒ System Variables ⇒ New ⇒ In "Variable name", enter "CLASSPATH" ⇒ In "Variable value", enter ".;path1\xxx.jar;path2\yyy.jar", where the entries are separated by a semi-colon (;).
    • For Linux and Mac OS: Edit ~/.profile or ~/.bash_profile (or /etc/profile for system-wide setting) to include the following line at the end of the file:
      export CLASSPATH=.:path1/xxx.jar:path2/yyy.jar
      The entries are separated by colon (:).
  3. You can also set the CLASSPATH in the javac/java command-line via the option -cp <paths> (or -classpath <paths>), for example,
    // For Windows
    // Compile Java source code
    javac -cp .;path1\xxx.jar;path2\yyy.jar
    // 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
    // Run Java class
    java -cp .:path1/xxx.jar:path2/yyy.jar ClassName
External Native Libraries (".lib", ".dll", ".a", ".so")

Some external package may provide static or shared native libraries in the form of ".lib" (Windows' static LIBrary), ".dll" (Windows' Dynamically Link Library), ".a" (Unix's static (Archive) library), or ".so" (Unix's Shared Object library).

Native Libraries are to be kept in a directory accessible via JRE's Property "java.library.path", which normally but not necessarily includes all the directories in the PATH environment variable.

Native libraries are not involved in the compilation. But if they are not properly included during runtime time, you will get a runtime error "java.lang.UnsatisfiedLinkError: no xxx in java.library.path".

To include external native libraries:

  1. Copy the native libraries into a system library directory, e.g., c:\windows\system32 (Windows), /usr/lib or /usr/local/lib (Mac OS X / Unix). You can verify that the directory is included in Java's System Property "java.library.path", via System.out.println(System.getProperty("java.library.path")).
  2. You can also set the native library path via the java's command-line option -Djava.library.path=xxx, for example,
    // Run a Java program
    java -Djava.library.path=xxx ClassName

Using an IDE can greatly simplifies inclusion of external packages. Read "Eclipse How-To" or "NetBeans How-To".

Link to References & Resources