“Java.lang.UnsatisfiedLinkError: no opencv_java320

2020-06-04 03:12发布

问题:

I have a selenium test that when it finishes makes some operations with OpenCV. With IntelliJ IDEA it works fine, the operations process correctly, but when I try to execute through command line (for Jenkins use in the near future), I get the error mentioned above:

"Java.lang.UnsatisfiedLinkError: no opencv_java320 in java.library.path"

I read the other questions on here and I've set up the java.library.path to the path where the jar and dll files are, but the error still comes up and I'm running out of ideas.

Could you please help me?

Thanks!

回答1:

Please find below a working snippet. Which you need to adapt to your needs.

assume following file structure

libs\opencv_java320.dll
pom.xml
src\test\java\sub\optimal\OpenCVTest.java

pom.xml - the part for the testing

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <argLine>-Djava.library.path=${project.basedir}/libs/</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

sub\optimal\OpenCVTest.java

package sub.optimal;
import org.junit.Test;
public class OpenCVTest {
    @Test
    public void someOpenCVTest() {
        System.out.printf("java.library.path: %s%n",
                System.getProperty("java.library.path"));
        System.loadLibrary("opencv_java320");
    }    
}

run the test

mvn compile test

output

...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running sub.optimal.OpenCVTest
java.library.path: X:\develop\opencv-demo/libs/
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: ...
...


回答2:

This worked for me. I am using intellij on mac

import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Test {

 public static void main(String[] args){
    //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); - REMOVE THIS
    nu.pattern.OpenCV.loadShared(); //add this
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    System.out.println("mat = " + mat.dump());
 }
}

And dependency

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


回答3:

If you are using STS/Eclipse or any IDE then follow the steps to getting solve your unsatisfiedlinkerror-no-opencv-java320 error.

Window -> Preferences -> User Libraries -> New -> create new Library Like attached Image

Note:- Jar location and Native Library Location (opencv/build/java/x64) should be exactly like this while creating the new library.



回答4:

If you're using IntelliJ IDEA you should add OpenCV library as Native Library Location.

  1. Go to File > Project Structure
  2. From side panel select Modules and then select the Dependencies tab
  3. Click the + icon on the bottom to add a dependency. Then select the Add JARS or directories option.
  4. Then browse to the path where you installed the OpenCV and select build/bin/opencv-***.jar (or build/java/opencv-***.jar in some case) and click Open.

    ※ If you can not find that jar file. I predict that you forgot build OpenCV repo. Reference this introduction

  5. It will be shown as dependency in the window. Now, we also have to add the Native Library Location. To do that, double click on the opencv-***.jar

  6. Then click on the + icon to add the Native Library Location.
  7. And then browse to the location where you installed OpenCV and select build/lib(in somecase it will be build/java/x64). Now click Open.

Now, you can use System.loadLibrary(Core.NATIVE_LIBRARY_NAME); to load library as expected

Reference: How to set-up OpenCV in IntelliJ IDEA of Aadam

PS: You also can add java.library.path in VM option like -Djava.library.path={PATH_T0_LIBRARY}