- Trying to use DLL from Java (JNA). Unable to load library exception
- 5 Answers 5
- Java System.load() – Syntax & Examples
- load(String filename)
- Syntax
- Example 2 – load(String filename) – File Path Incorrect
- Example 3 – load(filename) – Null filename
- Conclusion
- How to Handle the UnsatisfiedLinkError Runtime Error in Java
- UnsatisfiedLinkError Error: What is It & When does It Happen?
- How to Handle the UnsatisfiedLinkError Error
- UnsatisfiedLinkError Error Example
- Approach #1: Updating the PATH environment variable
- Approach #2: Verifying the java.library.path property
- Approach #3: Overriding the java.library.path property
- Approach #4: Using System.load() instead of System.loadLibrary()
- Summary
- Track, Analyze and Manage Errors With Rollbar
Trying to use DLL from Java (JNA). Unable to load library exception
I have NetBeans project from tutorial which causes exception: Exception in thread «main» java.lang.UnsatisfiedLinkError: Unable to load library ‘simpleDLL’: The specified module could not be found. Tried to put simpleDLL.dll in project libraries, copied file in system32 folder with no success.
Please check if the simpleDLL is 32 or 64 bit. Then check, if the JVM is also 32 or 64 bit. They have to be for the same platform.
5 Answers 5
I had exactly the same problem with loading a DLL, I solved it in this way:
- As Christian Kuetbach said, check if the simpleDLL you are using is compatible with your processor’s architecture, a 32-bit DLL won’t work on a 64-bit machine, and also a 64-bit DLL won’t work on a 32-bit machine.
- If the DLL is compatible, then the problem may be in your java library path. I put my DLL into the user.dir directory and then I used this code: Set Java library path to user.dir or maybe another path you want:
String myLibraryPath = System.getProperty("user.dir");//or another absolute or relative path System.setProperty("java.library.path", myLibraryPath);
System.loadLibrary("libraryWithoutDLLExtension");
It worked for me, try it and tell me if it works for you.
I used this code after public static void main(String[] args) < . My library is located in user.dir. Same result - exception
Exception in thread «main» java.lang.UnsatisfiedLinkError: C:\Windows\System32\simpleDLL.dll: Can’t find dependent libraries
It is not in your user.dir, user.dir is the path where the java program is located, unless you have located it in C:\Windows\System32\ you don’t have your dll in user.dir
I tried commenting all the «main» code and replacing it with: System.loadLibrary(«simpleDLL»); And it says: Exception in thread «main» java.lang.UnsatisfiedLinkError: Can’t load IA 32-bit .dll on a AMD 64-bit platform It means that the dll has been found but it cannot be loaded because i have a x64 machine, it does not work with the example found in the site you linked, but with System.loadLibrary it works. I suggest you to try implementing a DLL with JNI instead of JNA, i always worked with JNI and didn’t have problems with DLL linking
Also, on another not, this doesn’t work for me. I followed all your steps and I also added additional System.setProperty(«jna.library.path», dir); . The dir is absolute path ( D:\programs\. ). There must be one more issue that can happen.
Java System.load() – Syntax & Examples
In this tutorial, we will learn about the Java System.load() function, and learn how to use this function to load a native library into System, with the help of examples.
load(String filename)
System.load() loads the native library specified by the filename argument.
The filename must be an absolute path to the file you would like to load.
Syntax
The syntax of load() function is
Java Program
Loading dll.. Loading finished.
Example 2 – load(String filename) – File Path Incorrect
In this example, we will provide an incorrect path as filename to load() method. load() method should throw java.lang.UnsatisfiedLinkError .
Java Program
Loading dll.. Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: C:\nothing.dll at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source) at java.base/java.lang.Runtime.load0(Unknown Source) at java.base/java.lang.System.load(Unknown Source) at Example.main(Example.java:6)
Example 3 – load(filename) – Null filename
In this example, we will provide a null value as filename to load() method. load() method should throw java.lang.NullPointerException .
Java Program
Loading dll.. Exception in thread "main" java.lang.NullPointerException at java.base/java.io.File.(Unknown Source) at java.base/java.lang.Runtime.load0(Unknown Source) at java.base/java.lang.System.load(Unknown Source) at Example.main(Example.java:6)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java System.load() function, and also learnt how to use this function with the help of examples.
How to Handle the UnsatisfiedLinkError Runtime Error in Java
A native library is a library containing code compiled for a specific (native) architecture. There are certain scenarios like hardware-software integrations and process optimizations where using libraries written for different platforms can be very useful or even necessary. For this purpose, Java provides the Java Native Interface (JNI), which allows Java code that runs inside a Java Virtual Machine (JVM) to interoperate with applications and libraries written in other programming languages, such as C, C++, and assembly. The JNI enables Java code to call and be called by native applications and libraries written in other languages and it enables programmers to write native methods to handle situations where an application cannot be written entirely in Java [1].
Common native library formats include .dll files on Windows, .so files on Linux and .dylib files on macOS platforms. The conventional idiom for loading these libraries in Java is presented in the code example below.
package rollbar; public class ClassWithNativeMethod < static < System.loadLibrary("someLibFile"); >native void someNativeMethod(String arg); /*. */ >
Java loads native libraries at runtime by invoking the System.load() or the System.loadLibrary() method. The main difference between the two is that the latter doesn’t require the absolute path and file extension of the library to be specified—it relies on the java.library.path system property instead. To access native methods from the loaded libraries, method stubs declared with the native keyword are used.
UnsatisfiedLinkError Error: What is It & When does It Happen?
If a Java program is using a native library but is unable to find it at runtime for some reason, it throws the java.lang.UnsatisfiedLinkError runtime error. More specifically, this error is thrown whenever the JVM is unable to find an appropriate native-language definition of a method declared native , while attempting to resolve the native libraries at runtime [2]. The UnsatisfiedLinkError error is a subclass of the java.lang.LinkageError class which means this error is captured at program startup, during the JVM’s class loading and linking process.
Some commonly encountered situations where this error occurs include a reference to the ocijdbc10.dll and ocijdbc11.dll libraries when trying to connect to an Oracle 10g or 11g database with the OCI JDBC driver [3], as well as dependence on the lwjgl.dll library used in game development and Java applications relying on some core legacy C/C++ libraries [4].
How to Handle the UnsatisfiedLinkError Error
To figure out the exact culprit and fix the UnsatisfiedLinkError error, there are a couple of things to consider:
- Make sure that the library name and/or path are specified correctly.
- Always call System.load() with an absolute path as an argument.
- Make sure that the library extension is included in the call to System.load() .
- Verify that the java.library.path property contains the location of the library.
- Check whether the PATH environment variable contains the path to the library.
- Run the Java program from a terminal with the following command: java -Djava.library.path=»» -jar
An important thing to keep in mind is that System.loadLibrary() resolves library filenames in a platform-dependent way, e.g. the code snippet in the example in the Introduction would expect a file named someLibFile.dll on Windows, someLibFile.so on Linux, etc.
Also, the System.loadLibrary() method first searches the paths specified by the java.library.path property, then it defaults to the PATH environment variable.
UnsatisfiedLinkError Error Example
The code below is an example of attempting to load a native library called libraryFile.dll with the System.loadLibrary() method on a Windows OS platform. Running this code throws the UnsatisfiedLinkError runtime error with a message that reads “no libraryFile in java.library.path” suggesting that the path to the .dll library could not be found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package rollbar; public class JNIExample < static < System.loadLibrary("libraryFile"); >native void libraryMethod(String arg); public static void main(String. args) < final JNIExample jniExample = new JNIExample(); jniExample.libraryMethod("Hello"); >>
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libraryFile in java.library.path: C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files\Git\cmd;C:\Program Files (x86)\PuTTY\;C:\WINDOWS\System32\OpenSSH\;. 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) at rollbar.JNIExample.(JNIExample.java:6)
There are a couple of approaches to fixing this error.
Approach #1: Updating the PATH environment variable
One is to ensure that the PATH environment variable contains the path to the libraryFile.dll file. In Windows, this can be done by navigating to Control Panel → System Properties → Advanced → Environment Variables, finding the PATH variable (case insensitive) under System Variables, and editing its value to include the path to the .dll library in question. For instructions on how to do this on different operating systems, see [5].
Approach #2: Verifying the java.library.path property
Another approach is to check whether the java.library.path system property is set and if it contains the path to the library. This can be done by calling System.getProperty(«java.library.path») and verifying the content of the property, as shown in the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package rollbar; public class JNIExample < static < var path = System.getProperty("java.library.path"); if (path == null) < throw new RuntimeException("Path isn't set."); >var paths = java.util.List.of(path.split(";")); //paths.forEach(System.out::println); if (!paths.contains("C:/Users/Rollbar/lib")) < throw new RuntimeException("Path to library is missing."); >System.loadLibrary("libraryFile"); > native void libraryMethod(String arg); public static void main(String. args) < final JNIExample jniExample = new JNIExample(); jniExample.libraryMethod("Hello"); >>
Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: Path to library is missing. at rollbar.JNIExample.(JNIExample.java:16)
Technically, the java.library.path property can be updated by calling System.setProperty(«java.library.path», «./lib») , but since system properties get loaded by the JVM before the class loading phase, this won’t have an effect on the System.loadLibrary(«libraryFile») call that tries to load the library in the example above. Therefore, the best way to solve the issue is by following the steps outlined in the previous approach.
Approach #3: Overriding the java.library.path property
As an addendum to the previous approach, the only effective way to explicitly set the java.library.path property is by running the Java program with the —Dproperty=value command line argument, like so:
java -Djava.library.path=»C:\Users\Rollbar\lib» -jar JNIExample
And since this would override the system property if already present, any other libraries required by the program to run should also be included here.
Approach #4: Using System.load() instead of System.loadLibrary()
Lastly, replacing System.loadLibrary() with a call to System.load() which takes the full library path as an argument is a solution that circumvents the java.library.path lookup and fixes the problem regardless of what the initial cause for throwing the UnsatisfiedLinkError error was.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package rollbar; public class JNIExample < static < System.load("C:/Users/Rollbar/lib/libraryFile.dll"); >native void libraryMethod(String arg); public static void main(String. args) < final JNIExample jniExample = new JNIExample(); jniExample.libraryMethod("Hello"); System.out.println("Library method was executed successfully."); >>
Library method was executed successfully.
Hardcoding the path to the library might not be always desirable, however, so resorting to the other approaches might be preferable in those scenarios.
Summary
Using native libraries compiled for different platforms is a common practice in Java, especially when working with large and feature- or performance-critical systems. The JNI framework enables Java to do this by acting as a bridge between Java code and native libraries written in other languages. One of the issues programmers run into is failure to load these native libraries in their Java code correctly, at which point the UnsatisfiedLinkError runtime error is being triggered by the JVM. This article provides insight into the origins of this error and explains relevant approaches for dealing with it.
Track, Analyze and Manage Errors With Rollbar
![Rollbar in action](https://rollbar.com/wp-content/uploads/2022/04/section-1-real-time-errors@2x-1-300×202.png)
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!