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:
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)
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));
}
}
Output
Contains the name: true
Longer than the name: true
Has an extension: true
How It Works
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.
Something is always added. At minimum an extension is appended, so the result is longer than the five characters of "hello" on every platform.
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));
}
}
Output
Known library ending: true
Inside the native folder: true
File name kept: true
How It Works
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.
One of three endings. The path ends with ".so", ".dll", or ".dylib" on the common platforms, so the combined endsWith test is true.
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");
}
}
}
Output
hello follows the rule: true
zip follows the rule: true
sqlite3 follows the rule: true
null: NullPointerException
How It Works
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".
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.
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.