7

I'm trying to add opencv to my Spring Boot/Maven project.

In order to use opencv library I have to provide native lib into java.library.path.

I have added following command into Eclipse VM arguments:

-Djava.library.path="D:/Projects/lib/opencv/x86/opencv_java2411.dll"

and got a following exception:

java.lang.UnsatisfiedLinkError: no opencv_java2411 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)

I'm also trying to access java.library.path directly from my code:

System.out.println(System.getProperty("java.library.path"));

and it shows provided path: D:/Projects/lib/opencv/x86/opencv_java2411.dll

What am I doing wrong ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

4 Answers4

10

I also face the same issue doing so i did below to solve the issue.When i ran ran java -jar openCV=project jar I got the same exception as below

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.UnsatisfiedLinkError: no opencv_java320 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at com.wso2telco.rnd.CamCapture.<init>(CamCapture.java:47)
at com.wso2telco.rnd.CamCapture.main(CamCapture.java:144)
... 5 more

so i did the below changes the project in the .java class i had this lines

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.loadLibrary("opencv_java320");

I removed those 2 lines and added the below line

nu.pattern.OpenCV.loadLocally();

in order add that line you need to have theses dependencies in pom.xml

<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-0</version>
</dependency>
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-4</version>
</dependency>

After doing the above modification i was able to run my program from terminal using mvn exec:java -Dexec.mainClass="com.rnd.CamCapture"

Sheshan Gamage
  • 574
  • 11
  • 19
5

-Djava.library.path shouldn't point to the DLL, but to the folder containing the DLL. e.g. -Djava.library.path=D:/Projects/lib/opencv/x86/

ipsi
  • 2,049
  • 18
  • 23
3

On windows, set environment path which include opencv_java***.dll

e.x.
{your OpenCV path}\opencv\build\java\x64
or
{your OpenCV path}\opencv\build\java\x86

same in linux or other OS.

gogog
  • 410
  • 5
  • 9
3

Just use

OpenCV.loadShared(); // tested on opencv-4.5.1-2

instead of

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
exadmin
  • 39
  • 1