Java System load() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.System

Definition

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
CallWhat 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

ParameterDescription
filenameAbsolute path of the native library file to load.

Return Value

Return ValueDescription
voidNothing. On success the library's native functions become available.

Compare with Similar Methods

Ways to find and load native code:

MethodHow it differs
loadLibrary(name)Takes a short name and searches java.library.path.
mapLibraryName(name)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());
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.io.File;

public class SystemLoadRelative {
    static String tryLoad(String path) {
        try {
            System.load(path);
            return "loaded";
        } catch (UnsatisfiedLinkError | NullPointerException e) {
            return e.getClass().getSimpleName();
        }
    }

    public static void main(String[] args) {
        String relative = "native" + File.separator + System.mapLibraryName("hello");
        System.out.println("Relative path is absolute: " + new File(relative).isAbsolute());
        System.out.println("load(relative): " + tryLoad(relative));
        System.out.println("load(null): " + tryLoad(null));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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));
    }
}

How It Works

  1. 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.
  2. No exception at all. Because System.load is only called when the file exists, this run never throws; nativeReady keeps its starting value false.
  3. 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.

Next: loadLibrary()

Load a native library by its short name, searched in java.library.path.

loadLibrary() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful