Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv Java exception

Tags:

java

opencv

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
like image 964
andriy Avatar asked Oct 11 '12 13:10

andriy


4 Answers

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.

like image 198
OO7 Avatar answered Nov 11 '22 03:11

OO7


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.

like image 22
Eric Leschinski Avatar answered Nov 11 '22 03:11

Eric Leschinski


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.

like image 5
gidim Avatar answered Nov 11 '22 05:11

gidim


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.

like image 3
user3033420 Avatar answered Nov 11 '22 04:11

user3033420