10

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
andriy
  • 4,074
  • 9
  • 44
  • 71
  • Can you add some code where you integrate the opencv libs into java? Looks like the Classloader cannot find the library. – Fildor Oct 11 '12 at 13:37
  • 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 Oct 11 '12 at 13:46
  • 1
    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](http://en.wikipedia.org/wiki/Java_Native_Interface#How_the_JNI_works). – Fildor Oct 11 '12 at 13:56
  • This is the first time when I faced this problem. Do you know any jars, which wraps opencv dlls? – andriy Oct 11 '12 at 14:04
  • Sorry, no. I thought perhaps they are generated if you have something like opencv_java. But if you have `dll`s 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 (http://en.wikipedia.org/wiki/Java_Native_Interface#How_the_JNI_works). – Fildor Oct 11 '12 at 14:18
  • Possible duplicate of http://stackoverflow.com/questions/10509415/why-do-i-get-java-lang-unsatisfiedlinkerror-cant-find-dependent-libraries – Fildor Oct 11 '12 at 14:20
  • I think I know what's happening. If install OpenCV from EXE file, it doesn't provide necessary jars. At this moment I'm trying to install it from Git repository, as I understood, it is more up to date. I'll tell if it will work after reinstalling it. – andriy Oct 11 '12 at 15:22

6 Answers6

6

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
OO7
  • 2,785
  • 1
  • 21
  • 33
  • I tried doing the same thing but still I am getting this error. Is there any other work around? – Manpreet Nov 10 '14 at 06:12
  • @Manpreet Which version of OpenCV u r using ? Have u tried this procedure for `x86` & `x64` platforms ? What u did till now ? – OO7 Nov 10 '14 at 06:39
  • I am using OpenCV 2.4.9. I am on x86. I add the path of the dll in the PATH variable, I added its path under the Native library option of OpenCV 2.4.9 .jar file in the project. I am using System.loadLibrary("openCV_java249") to load the library in the code. – Manpreet Nov 10 '14 at 06:47
  • Have u tried `System.loadLibrary(Core.NATIVE_LIBRARY_NAME);` this code ? – OO7 Nov 10 '14 at 06:48
  • Even this is not working. The issue is, when I extracted the OpevCV 2.4.9 zipped file, I didn't get this dll file. Then I got opencv_java2410 from my friend and did System.loadLibrary("openCV_java2410") after adding its path in the native path of .jar. It worked fine as a desktop application, but then when I changed it to a web application, its not working. – Manpreet Nov 10 '14 at 06:52
  • 1
    @Manpreet Look at [Github link to OpenCV 2.4.9 Java dll](https://github.com/bestcool/OO7/tree/master/opencv-2.4.9%20dll/java). Try with these dll. Let me know, is this works for u ? – OO7 Nov 10 '14 at 07:52
  • Hi OO7, Thanks for the link. I downloaded the file and I don't see that error now. Now I am facing another error "UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0". I guess this is due to the .so files. Do you know the exact location where should I put all .so files in a web project? – Manpreet Nov 10 '14 at 16:11
  • I think u should look at ur own post [UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0 (OpenCV Java)](http://stackoverflow.com/questions/26849618/unsatisfiedlinkerror-org-opencv-highgui-highgui-imread-0-opencv-java) – OO7 Nov 11 '14 at 05:26
  • Lol... Yes. I found out the thing that was causing the issue and answered my own question. Thanks – Manpreet Nov 11 '14 at 07:30
  • Glad to know finally u get the solution. – OO7 Nov 11 '14 at 07:33
  • Is this too for Ubuntu? I only see .so files and no .dlls – Jakob Alexander Eichler Jan 24 '15 at 18:47
  • @tokam **.dlls are present x64 & x86 directories for 64-bit & 32-bit OS resp. Look at my comments to Manpreet**. I haven't tested this on Ubuntu. U can check out with these files & add comment on this. – OO7 Jan 29 '15 at 10:51
5

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • Making [OpenCV](http://opencv.org/) compilation obligatory in order to produce appropriate system dependent libraries for [Eclipse](https://eclipse.org/) is contrary to the idea of Java's portability. BTW: *OpenCV* compilation brings another problem if your GCC version is at least `5`: `/usr/include/host_config.h:105:2: error: #error -- unsupported GNU version! gcc 4.10 and up are not supported!`. Detailed Eclipse configuration description is available [here](http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/java_eclipse/java_eclipse.html#java-eclipse). – patryk.beza Apr 02 '16 at 22:37
5

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.

gidim
  • 2,314
  • 20
  • 23
3

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.

OO7
  • 2,785
  • 1
  • 21
  • 33
sagarshah
  • 51
  • 5
0

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);

mjk
  • 2,443
  • 4
  • 33
  • 33
0

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.

susan097
  • 3,500
  • 1
  • 23
  • 30