简体   繁体   中英

OpenCV 4.3.0 java.lang.UnsatisfiedLinkError in Eclipse

I'm trying to do some template matching with the Java binding of OpenCV 4.3.0 in Eclipse, but attempting to load the template image always results in this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.imgcodecs.Imgcodecs.imread_0(Ljava/lang/String;I)J

The line of code where this exception is thrown is this:

flowerTemplate = Imgcodecs.imread("/templates/flowerpot_white.png", Imgcodecs.IMREAD_COLOR);

I have tried a number of solutions suggested on similar questions on StackOverflow and elsewhere on the internet, including:

  • Pointing at the native library folder with the "Native library location" variable in the user library definition in Eclipse.
  • Adding the native library folder location to my PATH variable.
  • Adding the native library.dll location to my PATH variable.
  • Setting up the Eclipse run configuration to add the native library folder &.dll locations to the PATH and CLASSPATH variables.
  • Loading the library with the appropriate Java code, in each of the three ways I saw it suggested, in three different places which all run before the code that throws the exception.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load(<path_to_the_dll>);

File opencvLibrary = new File(System.mapLibraryName(Core.NATIVE_LIBRARY_NAME));
System.load(opencvLibrary.getAbsolutePath());
  • Placing the.dll in question into my source folder and every subfolder. I am running it from within Eclipse, so this is also the program's working directory.

UnsatisfiedLinkError is a runtime exception that happens when running your Java program. So placing your file in the source folder will not work.

You need it to be available in a place that your program can find it.

See this article for example:

https://www.javaworld.com/article/2077520/java-tip-23--write-native-methods.html

In it they place the library in Linux's library path. In windows you'd similarly place it in the current directory (where you're running from) or in some shared location.

This article explains Window's dll search order: https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

You shouldn't need to explicitly call System.loadLibrary() yourself. That's the library's responsibility.

Your problem is that OpenCV is improperly installed on your machine or inaccessible from Eclipse.

For instructions on how to make in work in Eclipse see:

Add.dll to java.library.path in Eclipse/PyDev Jython project

After removing every load method and then adding them back one-by-one, I determined that the issue was most likely caused by Eclipse loading the native library folder twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM