How to Handle java.lang.UnsatisfiedLinkError in Java?
Java.lang.UnsatisfiedLinkError is a subclass of LinkageError Class. When Java Virtual Machine(JVM) did not find the method Which is declared as “native” it will throw the UnsatisfiedLinkError.
Now let us do discuss when and why does it occur. Java.lang.UnsatisfiedLinkError occurs during the compilation of the program. It is because of the reason that compiler did not find the Native Library, a Library that contains native code which meant only for a specified operating system, Native library like .dll in Windows, .so in Linux and .dylib in Mac. The hierarchy of this Error is like the given below as follows:
Java.lang.Object Java.lang.Throwable Java.lang.Error Java.lang.LinkageError Java.lang.UnsatisfiedLinkError
As seen above now in order to handle this error, we need to make sure that the PATH should contain the given “DLL” file in Windows. We can also check the java.library.path is set or not. If we are running the java file using the Command Prompt in Windows we can use the Java -Djava.library.path=”NAME_OF_THE_DLL_FILE” -jar <JAR_FILR_NAME.jar> to run our java file. Another thing we can use is by giving the exact file location in System.LoadLibrary(“Exact File Path”) or System.load(“Exact File Path”) Method.
Output: When we run the above java file it will Compile Successfully and display the Output.
Hello from C file
We will generate the .dll file from this C file by using the command- ‘gcc cfile.c -I C:/Program Files/Java/jdk1.8.0_111/include -I C:/Program Files/Java/jdk1.8.0_111/include/win32 -shared -o cfile.dll‘. Now when we run the above java file it will Compile Successfully and display the Output.