Java System mapLibraryName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 2+

Definition

In Java, System.mapLibraryName(libname) returns the file name that a native library called libname has on the current operating system. It adds the platform's prefix and extension, nothing more; it does not check whether such a file exists.

This is exactly the name loadLibrary searches for, which makes it handy for error messages, build scripts, and building absolute paths for load.

A static method of the java.lang.System class, added in Java 2 (JDK 1.2). Call it on the class name, as in System.mapLibraryName(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass the short name and get the platform file name back:

Java
String file = System.mapLibraryName("hello");
// Linux: "libhello.so"   Windows: "hello.dll"   macOS: "libhello.dylib"
CallWhat it does
System.mapLibraryName("hello") on Linux"libhello.so"
System.mapLibraryName("hello") on Windows"hello.dll"
System.mapLibraryName("hello") on macOS"libhello.dylib"
System.mapLibraryName(null)Throws NullPointerException.

Added in Java 1.2. Other Unix-like systems generally follow the Linux rule. Because the result differs per platform, the examples print checks on it rather than the name itself.

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

Parameters

ParameterDescription
libnameShort library name, for example "hello".

Return Value

Return ValueDescription
StringThe platform-specific file name for that library.

Compare with Similar Methods

Where the mapped name is used:

MethodHow it differs
loadLibrary(name)Searches java.library.path for the mapped file name.
load(path)Needs a full path, which you can build from the mapped name.
lineSeparator()Another value that differs per operating system.

Java Examples

Example 1 — Inspect the Mapped Name Without Printing It

The exact file name changes per operating system, but some facts about it are true everywhere. Those are what the program prints.

Java
public class SystemMapLibraryNameCheck {
    public static void main(String[] args) {
        String file = System.mapLibraryName("hello");
        System.out.println("Contains the name: " + file.contains("hello"));
        System.out.println("Longer than the name: " + (file.length() > "hello".length()));
        System.out.println("Has an extension: " + (file.lastIndexOf('.') > 0));
    }
}

How It Works

  1. Always contains the input. Whether the result is "libhello.so", "hello.dll", or "libhello.dylib", the text "hello" is inside it, so contains is true.
  2. Something is always added. At minimum an extension is appended, so the result is longer than the five characters of "hello" on every platform.
  3. A dot after the name. lastIndexOf('.') finds the start of ".so", ".dll", or ".dylib". It is greater than 0 because the name comes first, so the last check prints true.

Example 2 — Combine the Mapped Name with a Folder

Put the mapped name into a File inside a "native" folder, the shape of path you would later pass to System.load.

Java
import java.io.File;

public class SystemMapLibraryNamePath {
    public static void main(String[] args) {
        String fileName = System.mapLibraryName("hello");
        File lib = new File("native", fileName);
        String path = lib.getPath();
        boolean knownEnding = path.endsWith(".so") || path.endsWith(".dll") || path.endsWith(".dylib");
        System.out.println("Known library ending: " + knownEnding);
        System.out.println("Inside the native folder: " + lib.getParent().equals("native"));
        System.out.println("File name kept: " + lib.getName().equals(fileName));
    }
}

How It Works

  1. Folder plus file name. new File("native", fileName) joins the two with the platform separator, giving something like native/libhello.so or native\hello.dll.
  2. One of three endings. The path ends with ".so", ".dll", or ".dylib" on the common platforms, so the combined endsWith test is true.
  3. Splitting it back. getParent() returns "native" and getName() returns the mapped file name unchanged, so both comparisons print true. Use getAbsolutePath() before passing it to load.

Example 3 — Check the Naming Rule for the Current OS

Rebuild the expected name from the operating system's naming rule and confirm that mapLibraryName agrees for several libraries.

Java
import java.util.Locale;

public class SystemMapLibraryNameRule {
    static String expected(String base) {
        String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
        if (os.startsWith("windows")) {
            return base + ".dll";
        }
        if (os.startsWith("mac")) {
            return "lib" + base + ".dylib";
        }
        return "lib" + base + ".so";
    }

    public static void main(String[] args) {
        String[] names = {"hello", "zip", "sqlite3"};
        for (String name : names) {
            System.out.println(name + " follows the rule: " + System.mapLibraryName(name).equals(expected(name)));
        }
        try {
            System.mapLibraryName(null);
        } catch (NullPointerException e) {
            System.out.println("null: NullPointerException");
        }
    }
}

How It Works

  1. The rule in code. expected reads os.name in lower case: Windows gets ".dll" with no prefix, macOS ("Mac OS X") gets "lib" plus ".dylib", and Linux and other Unix systems get "lib" plus ".so".
  2. Same answer, any OS. For each of the three names the JDK's result equals the rebuilt one, so every line prints true without revealing which operating system ran it.
  3. No name, no mapping. mapLibraryName(null) throws NullPointerException, which the catch turns into the last line.

Frequently Asked Questions

"libhello.so" on Linux, "hello.dll" on Windows, and "libhello.dylib" on macOS.
No. It only builds the name. Use new File(folder, name).exists() to check the disk.
You passed a name that already starts with "lib". Pass only the base name, such as "hello".
Yes. loadLibrary maps the short name this way and then searches java.library.path for the resulting file name.
It throws NullPointerException.

Next: nanoTime()

A high-resolution timer value in nanoseconds for measuring elapsed time.

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

14 people found this page helpful