Downloading and Installing JDK
8. (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:
- 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
".
java.ext.dirs
". You can print its contents viaSystem.out.println(System.getProperty("java.ext.dirs"))
. - For Windows, the JDK extension directory is located at "
- 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 theCLASSPATH
, 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 (:
). - For Windows, set the
- You can also set the
CLASSPATH
in thejavac
/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 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
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:
- 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
", viaSystem.out.println(System.getProperty("java.library.path"))
. - 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
Eclipse/NetBeans
Using an IDE can greatly simplifies inclusion of external packages. Read "Eclipse How-To " or "NetBeans How-To ".