In Java, System.load(filename) loads a native library, a file of compiled C or C++ code such as .dll, .so, or .dylib, from the exact file you name. After loading, Java methods declared native can call into it through JNI.
The argument must be an absolute path, for example /opt/app/libfast.so or C:\app\fast.dll. Java does not search any folders. To load by short name from the library search path, use loadLibrary.
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.load(...). No import is needed, because classes in java.lang are always visible.
Syntax
Build the full path, then load it once, usually in a static block:
Java
File lib = new File(dir, System.mapLibraryName("fast")).getAbsoluteFile();
System.load(lib.getAbsolutePath()); // UnsatisfiedLinkError if it cannot be loaded
Call
What it does
System.load("/opt/app/libfast.so")
Loads that exact file on Linux.
System.load("C:\\app\\fast.dll")
Same on Windows; backslashes are doubled in Java strings.
System.load("libfast.so")
Relative path: always UnsatisfiedLinkError.
file.getAbsolutePath()
Turns a File into the absolute path load needs.
Since Java 1.0. Loading the same library again in the same class loader does nothing.
Library declarations
Java
// java.lang.System
public static void load(String filename)
Parameters
Parameter
Description
filename
Absolute path of the native library file to load.
Return Value
Return Value
Description
void
Nothing. On success the library's native functions become available.
Builds the platform file name, such as "libfast.so".
Runtime.getRuntime().load(path)
The method that System.load calls.
Java Examples
Example 1 — Load an Absolute Path That Does Not Exist
The path is absolute, so Java tries the file and fails because it is missing. The example prints only true/false facts, never the path itself.
Java
import java.io.File;
public class SystemLoadMissing {
public static void main(String[] args) {
File dir = new File(System.getProperty("java.io.tmpdir"));
File lib = new File(dir, System.mapLibraryName("codetofun_missing_12345")).getAbsoluteFile();
System.out.println("Absolute path: " + lib.isAbsolute());
System.out.println("File exists: " + lib.exists());
try {
System.load(lib.getAbsolutePath());
System.out.println("Loaded");
} catch (UnsatisfiedLinkError e) {
System.out.println("Not loaded: " + e.getClass().getSimpleName());
}
}
}
Output
Absolute path: true
File exists: false
Not loaded: UnsatisfiedLinkError
How It Works
A full path in the temp folder.java.io.tmpdir gives the temporary directory and mapLibraryName adds the platform's prefix and ending. getAbsoluteFile() guarantees the result is absolute.
Facts, not folders. The real path differs on every computer, so the program prints isAbsolute() and exists() instead: true, and false because no such library was ever created.
The Error is caught.System.load accepts the absolute path, cannot find the file, and throws UnsatisfiedLinkError. The catch names that class, so the program ends normally.
Example 2 — Why a Relative Path Is Rejected
A relative path is refused before Java even looks for the file, and null fails too. One helper reports which Throwable each call produced.
Relative path is absolute: false
load(relative): UnsatisfiedLinkError
load(null): NullPointerException
How It Works
A path relative to the working folder. "native" plus the separator and the mapped name, such as native/libhello.so, has no root or drive letter, so isAbsolute() is false.
Rejected up front.System.load checks the path first and throws UnsatisfiedLinkError ("Expecting an absolute path") for any relative name, even if such a file exists in the current folder.
null is a different failure. With null there is no path to check, so the result is NullPointerException. The multi-catch handles both, because one is an Error and the other a RuntimeException.
Example 3 — Load Native Code Only If the File Is There
Check the file before calling load and fall back to plain Java when it is absent, a common pattern for optional speed-ups.
Java
import java.io.File;
public class SystemLoadOptional {
static boolean nativeReady = false;
static void loadIfPresent(File lib) {
if (!lib.isFile()) {
System.out.println("No native file, using the Java version");
return;
}
System.load(lib.getAbsolutePath());
nativeReady = true;
}
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
File lib = new File(System.getProperty("user.dir"), System.mapLibraryName("codetofun_fast_12345")).getAbsoluteFile();
loadIfPresent(lib);
System.out.println("Native code active: " + nativeReady);
System.out.println("square(7) in Java: " + square(7));
}
}
Output
No native file, using the Java version
Native code active: false
square(7) in Java: 49
How It Works
Look before loading. The library is expected in the working directory (user.dir). isFile() is false because it was never built, so loadIfPresent prints a notice and returns early.
No exception at all. Because System.load is only called when the file exists, this run never throws; nativeReady keeps its starting value false.
The Java path does the work. The program still produces its answer with the plain Java square method: 7 times 7 is 49. With a real library in place, the native version could be used instead.
Frequently Asked Questions
load needs the full absolute path of the file. loadLibrary takes a short name like "fast", maps it to the platform file name, and searches java.library.path.
You passed a relative path. Convert it first, for example with new File(path).getAbsolutePath().
No. UnsatisfiedLinkError is an Error, not an Exception. Catch UnsatisfiedLinkError itself.
The second call does nothing; a library is loaded only once per class loader.
Common reasons are a 32-bit versus 64-bit mismatch, a library built for another operating system, or a missing dependency library.