10
votes

I'm using Windows 7 and getting this exception when trying to run a Java project that uses opencv libraries:

Exception in thread "main" 
java.lang.UnsatisfiedLinkError: no opencv_java in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.opencv.highgui.Highgui.<clinit>(Highgui.java:416)
at teste.main(teste.java:21)

What did I do wrong? Is some import missing?

I want to create a simple Java project in Eclipse (not Android), that uses openCV.

  • So I've extracted javacv from OpenCV-2.4.2.exe file to C:\
  • Then executed "cmake -G "MinGW Makefiles" -DBUILD_opencv_java=ON C:\opencv" command and after that, "mingw32-make". Everything was build without errors or warnings
  • After I've added opencv dll's to my Environment Variables
6
Can you add some code where you integrate the opencv libs into java? Looks like the Classloader cannot find the library. - Fildor
In Run Configurations, I've added -Djava.library.path=C:\opencvFinal argument. Also in Java Build Path I've added External Class Folder which is C:\opencvFinal\bin, where all opencv dlls are saved. And when I'm calling System.loadLibrary("opencv_java"); it gives me exception. - andriy
Wait, do you have dlls, only? What you need is a jar that wraps those dlls. Or you need to wrap them yourself using jni. - Fildor
This is the first time when I faced this problem. Do you know any jars, which wraps opencv dlls? - andriy
Sorry, no. I thought perhaps they are generated if you have something like opencv_java. But if you have dlls only, then you have a lot of work to do. Or find a ready-built java integration. See, dll's are for C/C++. To use them in Java, you have to make use of the native interface (en.wikipedia.org/wiki/Java_Native_Interface#How_the_JNI_works). - Fildor

6 Answers

6
votes

This exception is raised because the system is trying to find native libraries that OpenCV needs for this specific platform, and does not find them.

Exception in thread "main" 
java.lang.UnsatisfiedLinkError: no opencv_java in java.

To solve this error:

  1. If you are using opencv version < opencv 2.4.6 then the answer given by @user3033420 is the solution.
  2. If you are using version >= opencv 2.4.6, then the jar has a constant variable in Core class named NATIVE_LIBRARY_NAME that you give to the loadLibrary() function in FaceDetector class to include features of opencv in your project, you probably already have this:

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    

Using the above named constant, there is no need to remember name of strang dll files.

Add the opencv 2.4.9.jar to the build path from project preferences as:

Window -> Preferences -> Java Build Path –> Add Library -> User Libraries -> 
User Library -> & click New

You will see a dialog as below. Add opencv-2.4.9 as Library Name & click Ok.

Create New User Library

Then Add External Jar & locate your opencv jar & click ok. Then expand opencv-2.4.9.jar & click on Native Library Location (None) as shown below :

Add Native Library Location of OpenCV

Necessary step: enter the location of the folder containing native library used by "opencv-2.4.9" & then click OK as shown below :

Native Library Folder Configuration

So now the jar now has all the native libraries it needs to do the work. Rebuild the java program and everything should compile and run as designed.

5
votes

If you get this error:

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

It probably means you are shooting from the hip, programming by brownian motion, trying to get openCV to work. Like trying to figure out how an airplane works in flight by pressing all the buttons furiously. You're going to have a bad time.

What the error means:

Eclipse is telling you that the jar file can't find libraries it needs to do its job. So naturally it's not going to work until you make them available. You've got to find a tutorial on "how to build openCV from source" on your particular platform: (windows, mac, linux, etc), (32bit, 64bit, etc).

Basically, you glossed over the 'Native library location' settings, or didn't set them correctly, and so the jar can't find its support libraries written in C.

How do fix it, thousand foot view:

  1. Download the source code for openCV for your operating system.
  2. Follow the directions to build openCV from source.
  3. Copy the jar into a lib directory in your Java project.
  4. Configure the jar to look for its native libraries by setting the "native library location" to the build/lib directory under the path where you built openCV from source.
  5. Clean build the java project, and the UnsatisfiedLinkError should go away.

This blog talks about the steps above in step-by-step detail: https://udallascs.wordpress.com/2014/03/30/adding-opencv-and-configuring-to-work-with-eclipse-and-java/

Why can't this just be a simple jar?

Because most of openCV is written in the C programming language. And the jar file you are using is just a window into that C world. So it's a rube Goldberg machine. You'll see these sorts of things all over the place in the real work world, so pay attention, you are getting an education here.

5
votes

Trying to load the library by doing this:

 System.loadLibrary("opencv_java244")

Or by doing this:

 System.loadLibrary("opencv_java244")

Didn't work - still got the same error.

Finally what worked was providing the full path to the dylib file and using:

System.load(new File("/usr/local/Cellar/opencv/2.4.10.1/share/OpenCV/java/libopencv_java2410.dylib").getAbsolutePath());'

I'm using HomeBrew but however you installed it just find the file and uddate the path.

3
votes

I find solution. Actual dll is located at openCV\opencv\build\java\x64\ folder. In my case its name is opencv_java247.dll , So i have changed java code line

System.loadLibrary("opencv_java244") 

to

System.loadLibrary("opencv_java247") 

I also set native library location as E:/Sagar_tools/tools/openCV/opencv/build/java/x64 (full path to dll) in build path.

0
votes

The function loadlibrary tries to find the name of the the DLL in your PATH variable -- check the DLL name. You can also try System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

0
votes

In that case, you should give the path for jvm to where the opencv dll file is intalled as : -Djava.library.path="C:\opencv\build\java\x64" and add the code as : System.loadLibrary(Core.NATIVE_LIBRARY_NAME); As, i try this in my netbeans ide and my problem is solved.