In Java, System.loadLibrary(libname) loads a native library by its short name. Java turns the name into a file name with mapLibraryName ("fast" becomes libfast.so, fast.dll, or libfast.dylib) and looks for that file in the folders of the java.library.path system property.
This is the usual way JNI code is loaded, because the same Java line works on every operating system. You only ship the right file per platform and point -Djava.library.path=... at its folder.
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.loadLibrary(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the bare name, with no prefix, extension, or folder:
java.library.path set: true
Not found: UnsatisfiedLinkError
How It Works
The search list exists. The JVM always fills in java.library.path at startup. Its folders differ per computer, so only the != null check is printed.
Searched and not found.loadLibrary maps the name to a file name and tries each folder of the path in order. None contains it, so it throws.
The familiar message. The error class is UnsatisfiedLinkError; its message (not printed here) reads like "no codetofun_missing_12345 in java.library.path", which is the error JNI beginners usually meet first.
Example 2 — Try the Library Once in a Static Block
Load the library when the class initializes, remember whether it worked, and use a pure Java version when it did not.
Java
public class SystemLoadLibraryStatic {
static final boolean NATIVE;
static {
boolean ok;
try {
System.loadLibrary("codetofun_fastmath_12345");
ok = true;
} catch (UnsatisfiedLinkError e) {
ok = false;
}
NATIVE = ok;
}
static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println("Native library loaded: " + NATIVE);
System.out.println("Using: " + (NATIVE ? "native code" : "pure Java"));
System.out.println("10! = " + factorial(10));
}
}
Output
Native library loaded: false
Using: pure Java
10! = 3628800
How It Works
Runs before main. The static block executes when the class is initialized, so the load is attempted exactly once, before main prints anything.
A final flag. The local ok records the outcome and is copied into the static final field NATIVE. The library does not exist, so the flag is false.
Graceful fallback. The program picks "pure Java" and computes 10! with a loop: 2 times 3 times ... times 10 is 3628800. A program with real native code would call its native method only when NATIVE is true.
Example 3 — Which Library Names Are Accepted
Four arguments show the naming rules: the short name is right, a "lib" prefix just searches for the wrong file, a folder is rejected, and null fails.
plain name: UnsatisfiedLinkError
with lib prefix: UnsatisfiedLinkError
with a folder: UnsatisfiedLinkError
null: NullPointerException
How It Works
The right form still fails here. "codetofun_demo_12345" is the correct style of name, but no such library is installed, so the search ends in UnsatisfiedLinkError.
Doubled prefixes. Adding "lib" yourself is a classic mistake: on Linux Java would look for "liblibcodetofun_demo_12345.so". A folder in the name is refused at once, before any search.
Labels instead of paths. The program prints a fixed label for each attempt, because the folder name uses the platform's separator. The null name gives NullPointerException.
Frequently Asked Questions
Put the library file in a folder and start Java with -Djava.library.path=thatFolder, or use System.load with the absolute path. Check that the file name matches System.mapLibraryName("X").
Just "fast". Java adds the platform prefix and extension itself.
Calling System.setProperty("java.library.path", ...) after startup usually has no effect, because the JVM reads the value once. Set it on the command line instead.
In a static block of the class that declares the native methods, so it runs once before any native method is used.
The Java call is the same. Only the library files differ: .dll on Windows, .so on Linux, .dylib on macOS.