Java System.loadLibrary() – Examples
In this tutorial, we will learn about the Java System.loadLibrary() function, and learn how to use this function with the help of examples.
loadLibrary(String libname)
System.loadLibrary(libname) loads the native library specified by the libname
argument.
Make sure the library file is present in any of the path specified by java.library.path.
Syntax
The syntax of loadLibrary() function is
loadLibrary(String libname)
where
Parameter | Description |
---|---|
libname | The name of library. |
Returns
The function returns void.
Example 1 – loadLibrary(libname)
In this example, we will load a dll present in the Java library path.
awt.dll
is present in the location C:\Program Files\Java\jre-10.0.1\bin
. And this location is part of java.library.path
.
Java Program
public class Example {
public static void main(String args[]) {
System.out.println("Loading Library...");
String libname = "awt";
System.loadLibrary(libname);
System.out.println("Library Loaded.");
}
}
Output
Loading Library...
Library Loaded.
Example 2 – loadLibrary() – With Extension
In this example, we will provide the extension as well for the library name to loadLibrary() method. loadLibrary() throws java.lang.UnsatisfiedLinkError
. So, do not provide the extension in the library name.
Java Program
public class Example {
public static void main(String args[]) {
System.out.println("Loading Library...");
System.loadLibrary("awt.dll");
System.out.println("Library Loaded.");
}
}
Output
Loading Library...
Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt.dll in java.library.path: [C:\Program Files\Java\jre-10.0.1\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, ., .]
at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
at java.base/java.lang.System.loadLibrary(Unknown Source)
at Example.main(Example.java:5)
Example 3 – loadLibrary(libname) – Null libname
In this example, we will pass null
as argument for library name to loadLibrary() method. loadLibrary() throws java.lang.NullPointerException
. Make sure that the library name is not null.
Java Program
public class Example {
public static void main(String args[]) {
System.out.println("Loading Library...");
String libname = null;
System.loadLibrary(libname);
System.out.println("Library Loaded.");
}
}
Output
Loading Library...
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
at java.base/java.lang.System.loadLibrary(Unknown Source)
at Example.main(Example.java:5)
Example 4 – loadLibrary(libname) – Library not present
In this example, we will try to load a library which is not present anywhere in the Java library paths. In such cases, loadLibrary() throws java.lang.UnsatisfiedLinkError
.
Java Program
public class Example {
public static void main(String args[]) {
System.out.println("Loading Library...");
String libname = "nolib";
System.loadLibrary(libname);
System.out.println("Library Loaded.");
}
}
Output
Loading Library...
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nolib in java.library.path: [C:\Program Files\Java\jre-10.0.1\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, ., .]
at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
at java.base/java.lang.System.loadLibrary(Unknown Source)
at Example.main(Example.java:5)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java System.loadLibrary() function, and also learnt how to use this function with the help of examples.