Search code examples
javaopencvgradleunsatisfiedlinkerror

java.lang.UnsatisfiedLinkError: OpenCV library not found in SpringBoot with Gradle


I added OpenCV dependency to my build.gradle file

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  implementation 'org.openpnp:opencv:4.5.1-1'
}

when running gradle build, the library is downloaded and build works fine. But when I run the code

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.opencv.core.Core;
import org.opencv.core.Mat;



@SpringBootApplication
public class AiApplication {

   public static void main(String[] args) {
       SpringApplication.run(AiApplication.class, args);
       System.out.print("Hello world");
       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
       Mat mat = Mat.eye(3, 3, 0);
       System.out.println("OpenCV Mat:\n" + mat.dump());
   }}

I get an error

java.lang.UnsatisfiedLinkError: no opencv_java451 in java.library.path:

So I located my Gradle file opencv-4.5.1-1.jar and run

java -Djava.library.path=/home/pescador/.gradle/caches/modules-2/files-2.1/org.openpnp/opencv/4.5.1-1/31b0b04ae7565a746a15c930c3a246c2655da00c -jar ./build/libs/ai-0.0.1-SNAPSHOT.jar 

but with the same error. I guess I have to add some other -Djava.library.path. I do not want to use any IDE, my project run in terminal on Linux. So any advise how to setup eclipse or IntelliJ will not help. Thank you


Solution

  • I just switched to maven.

        <dependency>
            <groupId>org.openpnp</groupId>
            <artifactId>opencv</artifactId>
            <version>4.9.0-0</version>
        </dependency>
    

    and java

    static{
        OpenCV.loadLocally();
    }
    public static void main(String[] args) throws InterruptedException {
    ...