What is the Runtime.loadLibrary() function in Java?

This static loadLibrary() function loads the native library defined by the libname parameter. This method helps to load the dynamic library.

If there is any security manager, its checklink() function will be called with the libname parameter of the loadLibrary() function. It may cause a security exception. The loadLibrary() function is an easy and conventional way of invoking this method.

Syntax


void loadLibrary(String libname)

Parameters

libname: This is the name of the library.

Return value

Its void method does not return anything.

  • If the libname is null, the Null Pointer Exception is thrown. Another exception named Unsatisfied Link Error occurs if the library is not available.

Example

public class EdPresso {
public static void main(String args[]) {
System.out.println("Library is loading");
String libname = "awt";
System.loadLibrary(libname);
System.out.println("library is completely loaded");
}
}

Errors

  • Demo#1: In the following code, if both extension and name of the library are directly provided to the loadLibrary() function.
  • Demo#1: If null is passed to the loadLibrary() function as a parameter for the name of the library.
public class loadlibrary_example2 {
public static void main(String args[]) {
System.out.println("Library is loading");
System.loadLibrary("foo.dll");
System.out.println("library is completely loaded");
}
}

Free Resources