Java System loadLibrary() Method

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

Definition

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
static {
    System.loadLibrary("fast");  // libfast.so / fast.dll / libfast.dylib
}
CallWhat it does
System.loadLibrary("fast")Correct: just the name.
System.loadLibrary("libfast.so")Wrong: searches for "liblibfast.so.so" and fails.
System.loadLibrary("lib/fast")Wrong: a folder separator is rejected.
java -Djava.library.path=native AppAdds the folder Java searches.

Since Java 1.0. Usually called in a static block of the class with the native methods, so the library is loaded once, when the class is first used.

Library declarations
Java
// java.lang.System
public static void loadLibrary(String libname)

Parameters

ParameterDescription
libnameShort library name, without "lib", without an extension, and without folders.

Return Value

Return ValueDescription
voidNothing; the library is loaded or an error is thrown.

Compare with Similar Methods

How it relates to the other native-library methods:

MethodHow it differs
load(path)Loads one exact file by absolute path; no searching.
mapLibraryName(name)Shows which file name loadLibrary will look for.
Runtime.getRuntime().loadLibrary(name)What System.loadLibrary calls internally.

Java Examples

Example 1 — Load a Library That Is Not on the Path

A made-up name is searched for in every folder of java.library.path, is not found, and produces the typical error.

Java
public class SystemLoadLibraryMissing {
    public static void main(String[] args) {
        String searchPath = System.getProperty("java.library.path");
        System.out.println("java.library.path set: " + (searchPath != null));
        try {
            System.loadLibrary("codetofun_missing_12345");
            System.out.println("Loaded");
        } catch (UnsatisfiedLinkError e) {
            System.out.println("Not found: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

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

How It Works

  1. Runs before main. The static block executes when the class is initialized, so the load is attempted exactly once, before main prints anything.
  2. 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.
  3. 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.

Java
import java.io.File;

public class SystemLoadLibraryNames {
    static String attempt(String name) {
        try {
            System.loadLibrary(name);
            return "loaded";
        } catch (UnsatisfiedLinkError | NullPointerException e) {
            return e.getClass().getSimpleName();
        }
    }

    public static void main(String[] args) {
        String base = "codetofun_demo_12345";
        String[] labels = {"plain name", "with lib prefix", "with a folder", "null"};
        String[] names = {base, "lib" + base, "folder" + File.separator + base, null};
        for (int i = 0; i < names.length; i++) {
            System.out.println(labels[i] + ": " + attempt(names[i]));
        }
    }
}

How It Works

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

Next: mapLibraryName()

Turn a short library name into the platform file name, such as libhello.so or hello.dll.

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

13 people found this page helpful