Home »
Java programming language
Java System class loadLibrary() method with example
System class loadLibrary() method: Here, we are going to learn about the loadLibrary() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 15, 2019
System class loadLibrary() method
- loadLibrary() method is available in java.lang package.
- loadLibrary() method is used to load the library with the given parameter named library_name(library name) as an argument passed in the method.
- A java file may contain native code for that we need to load the library by using loadLibrary() method. In this method, the mapping from a library_name to a given file name is done in a system-specific manner. At the time of class loading and instantiating, the required implemented native code for the native methods will then be loaded as well.
- loadLibrary() method is a static method, it is accessible with the class name too.
-
loadLibrary() method may throw an exception at the time of library loading:
- SecurityException: In this exception, its checkLink() method does not allow load the specified library as passed in the method when the security manager exists.
- UnsatisfiedLinkError: In this exception, if the loaded library does not exists.
- NullPointerException: In this exception, if the loaded library is null.
Syntax:
public static void loadLibrary(String library_name);
Parameter(s):
- String library_name – represents the name of the library.
Return value:
The return type of this method is void, it does not return anything.
Example:
// Java program to demonstrate the example of
// loadLibrary() method of System Class
public class LoadLibraryMethod {
public static void main(String[] args) {
// load a library FP30TXT.dll that is in Windows/system folder*/
System.out.println(" Process of Library Loading ");
Runtime.getRuntime().loadLibrary("C:/Windows/system/FP30TXT.dll");
System.out.println("Process of Library Loading completion");
}
}
Output
E:\Programs>javac LoadLibraryMethod.java
E:\Programs>java LoadLibraryMethod
Process of Library Loading
Process of Library Loading completion