System.loadLibrary(): This method is available in java.lang.System class of Java.
Syntax:
void java.lang.System.loadLibrary(String libname)
This method takes one argument of type String as its parameter. This method loads the native library specified by the libname argument. The libname argument must not contain any platform-specific prefix, file extension, or path.
Parameters: One parameter is required for this method.
libname: the name of the library.
Throws:
1. SecurityException - if a security manager exists and its checkLink method doesn't allow loading of the specified dynamic library.
2. UnsatisfiedLinkError - if either the libname argument contains a file path, the native library is not statically linked with the VM or the library cannot be mapped to a native library image by the host system.
3. NullPointerException - if libname is null
Approach 1: When no exceptions.
Java
public class SystemloadLibrary {public static void main(String[] args) {String libname = "C:/Windows/System64/cryptsvc.dll";System.loadLibrary(libname);System.out.println("Library loaded successfully");}}
Output:
Library loaded successfully
Approach 2: UnsatisfiedLinkError
Java
public class SystemloadLibrary {public static void main(String[] args) {String libname = "D://hello";System.loadLibrary(libname);System.out.println("Library loaded successfully");}}
Output:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no D://hello in java.library.path: at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2447) at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:809) at java.base/java.lang.System.loadLibrary(System.java:1893)
Approach 3: NullPointerException
Java
public class SystemloadLibrary {public static void main(String[] args) {String libname = null;System.loadLibrary(libname);System.out.println("Library loaded successfully");}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "libname" is null at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:805) at java.base/java.lang.System.loadLibrary(System.java:1893)
No comments:
Post a Comment