0

I am working on an application that runs on Linux. I have created a singleton class (Sample.java) and I want to load the opencv.so only when an instance of this class is created. Below is the code for the same:-

synchronized public static Sample getInstance() {
    if (null == __instance) {
        __instance = new Sample();
    }
    return __instance;
}

private Sample() {
    try {
        System.load(<absolute_path_to_opencv.so>);
    }
    catch (Error ex) {
        // do nothing
    }
}

After re-deploying the ear file "null == __instance" evaluates to true and "System.load(<absolute_path_to_opencv.so>)" gives the below exception: -

java.lang.UnsatisfiedLinkError: Native Library <path>/opencv-4.5.2.so already loaded in another classloader

Since I have a catch block following the System.load(), the flow continues. But the next place in my code I use anything from library (Mat.class in the below stacktrace), it throws UnsatisfiedLinkError:-

java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J
        at org.opencv.core.Mat.n_Mat(Native Method)
        at org.opencv.core.Mat.<init>(Mat.java:23)
        at adapters.SampleManager.getColorTags(SampleManager.java:64)

What I do not understand is, if the library is already loaded, then why is it failing when I try to use it? Please help.

  • Take a look here, it might help: https://stackoverflow.com/questions/36936948/java-lang-unsatisfiedlinkerror-native-library-xxx-so-already-loaded-in-another – yakobom Jul 01 '21 at 05:29

0 Answers0