Skip to content
Related Articles

Related Articles

Improve Article

How to Handle java.lang.UnsatisfiedLinkError in Java?

  • Last Updated : 23 Sep, 2021
Geek Week

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

Example

Java




// Java Program to Illustrate UnsatisfiedLinkError
  
// Importing input output classes
import java.io.*;
  
// Main class
public class GFG {
  
    // Loading the External Library
    //"libfile" is name of C file
    static {
        System.loadLibrary("libfile");
    }
  
    //Method 1
    // To define externally in C file
    native void cfun();
  
    // Method 2
    // Main driver method
    public static void main(String[] args) {
        // Creating the object of above class inside main()
        GFG g = new GFG();
  
        // Calling over the above method
        g.cfun();
    }
}

Output:



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.

Example

Java




// Java Program to Resolve UnsatisfiedLinkError by
// considering C file where native function is defined
  
// Importing input output classes
import java.io.*;
  
// Main class
public class GFG {
  
    // Loading the External Library
    // "libfile" is name of C file
    static
    {
        System.load(
            "C:/Users/SYSTEM/Desktop/CODES/libfile.dll");
    }
  
    // Method 1
    // Defined externally in C file
    native void cfun();
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of class inside main()
        GFG g = new GFG();
  
        // Calling the above object
        g.cfun();
    }
}

Output: When we run the above java file it will Compile Successfully and display the Output.

Hello from C file

Note:

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :