scalaopencvdynamic-library

Link OpenCV library to a Scala project


I've been trying to run a Scala project that uses the library OpenCV. I am using maven to build the project and in the pom.xml file I have the following dependency:

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

In the code I also included the following line:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

But when I run the project I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java490 in java.library.path

Plus, the application I'm trying to run uses Spark and I am wondering if it is necessary to load the library on each node of the cluster or, once loaded in the main, the different nodes will be able to use the OpenCV classes implementation.

I'm sorry if the question is probably dumb, I've been trying to find the solution myself but my lack of knowledge stopped me from understanding what the real problem is. Thanks to anyone who will put effort in answering and if you could also explain the problem I would really appreciate it.

Please note that I already tried to fix the issue by building a fat jar (jar-with-dependencies) but I still get the same error. I've been trying to find a solution and, as far as I understood, the problem might be that the OpenCV library is not located in the system I am using. In fact, I read that people suggest linking the library using the absolute path: System.loadLibrary("path/to/library.so")

I've been trying to find a precompiled .so file but I cannot find one (I am unable to use cmake on the machine I'm using since I am connecting via ssh and I do not have privileges to download tools). I was thinking of compiling the source code on my local machine and then transferring the file to the remote machine but, as you might've understood, at this point I'm not sure of what I am doing.

EDIT

After @dmytro-mitin 's suggestions I was able to fix the previous error. But now I see that adding System.loadLibrary(Core.NATIVE_LIBRARY_NAME) didn't solve the runtime error I got before, which is:

Caused by: java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J


Solution

  • I was using Apache Spark to distribute the application on different nodes. The point was that while it was possible to find the library on the master node, each note was not able to find the implementation for OpenCV. I added the lines:

    nu.pattern.OpenCV.loadShared()
    System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME)
    

    in the function that is mapped on the different nodes and this fixed the issue.