4

I'm currently trying to set up OpenCV on my 32-bit laptop, but I keep getting an error message which is confusing me:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 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 hello.main(hello.java:8)

I've tried switching a few things around, like going into User Libraries and changing the Native Library Location to \x64 instead of \x86, but that hasn't worked. I've also made sure that my Java project has OpenCV-2.4.9 as one of its libraries, which it has. I've also tried 'cleaning' all projects, but that hasn't worked (does it ever?). So I've followed all the instructions in the set-up tutorial but it still isn't working.

Does anyone know what the problem might be here? Thanks in advance...

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Zetland
  • 569
  • 2
  • 11
  • 25
  • 3
    What is your OS ? What is the full name of your library file ? What is line 8 in hello.java ? What means *project has OpenCV-2.4.9 as one of its libraries* ? (did you put the native lib in classpath ? or something else ?) What is the value of the system variable `java.library.path` (just do `System.out.println(System.getProperty("java.library.path"));` ? Do you know the difference between a native library and a java library ? – ben75 Feb 21 '15 at 10:41
  • I'm using Windows 7. My library file's full name is opencv-249.jar. Line 8 is `System.loadLibrary( Core.NATIVE_LIBRARY_NAME );` Not sure about the native library question, but I put it in the bit that appears when you click down on 'opencv-249.jar. `java.library.path` = C:\Program Files\OpenCV\opencv\build\x86. Thanks for taking the time to write such a good comment. – Zetland Feb 21 '15 at 12:24

6 Answers6

11

The problem is that the native opencv library cannot be found because it is not in the java.library.path (it is a native library and it should not be in usual java classpath, but it must be in the java.library.path).

Your opencv distribution should contains:

  • a java library : opencv249.jar. This library is mainly a java to native library with jni stuff allowing you to do native calls from java code. It must be in the classpath (and according the error you have and your explanations : it is on your classpath --> everything is ok here)

  • a native library named libopencv_java249.so and this must be in your java.library.path . Obviously : it is not : the UnsatisfiedLinkError is the symptom of a missing native library. To include this native library: start your java program with the following parameter:

    -Djava.library.path=/path/to/the/directory/of/ibopencv_java249.so

ben75
  • 29,217
  • 10
  • 88
  • 134
  • Thanks for this. Where do I put the parameter though? Should it go in the main method? – Zetland Feb 21 '15 at 13:23
  • on the command line (if you are using command line) . If you are using an IDE (like eclipse) : there must be a field named *VM arguments* in your "Run configuration" (or something similar). (it is not program args : this is a JVM parameter) – ben75 Feb 21 '15 at 13:59
  • 1
    Thanks! I can't find `libopencv_java249.so` though. It doesn't seem to have been downloaded with OpenCV-2.4.9. Is this something that I need to download separately downloaded? – Zetland Feb 21 '15 at 14:31
  • Gosh!! I got so dumb... I importedd a project in NetBeans from my old lappy and forgot to change this path which was pointing to a non existent location on my brand new system. Thanks man.. I either need some coffee or some sleep... – Fr0zenFyr May 25 '15 at 20:33
3

Another possible solution which worked for me on Ubuntu: Instead of setting the JVM parameter to the exact libopen.so file, try setting it to the folder:

-Djava.library.path=/opencv-3.2.0/opencv/build/lib/
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
Ognjen Mišić
  • 1,219
  • 17
  • 37
0

good day!i saw this one from the other forum.

How to set the java.library.path from Eclipse

i found out that you should copy the .dll file from your opencv and paste it to your windows folder. to do that, kindly locate the .dll from this path folder

opencv>build>java>x64 (for 64bit platform) or opencv>build>java>x86 (for x86/32 bit platform).

copy the .dll file and paste it to your windows folder ( c:/windows ) after that you may recompile your work to check if it works.

*note this tutorial is applicable only if you already set the .jar file of opencv to the preference setting of eclipse.

this method works for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
clara
  • 1
  • 1
0

Using Java in Netbeans, when including

-Djava.library.path="E:\Program Files\opencv\build\java\x64" 

in the Project Property>Run I just forgot the double quotes between the path. Add double quotes then everything worked fine.

rensothearin
  • 670
  • 1
  • 5
  • 24
M. REYSEY
  • 1
  • 2
0

Hi if someone is still facing the same challenge on intelij, you can easily config this by first downloading OpenCv here then you can then click on file>Project Structure

snip Click on + sign as shown on the image Select Jars/directories Then navigate to the opencv installation folder as shown link then double click opencv-version.jar opencv

then click add add

Then select either 1 or 2 as shown below based on your OS os

After that click okay okay and then try to run the application again

This solution was inspired by this article

Jasper
  • 371
  • 3
  • 4
-1

I had the same issue on ubuntu 14.o and I struggled a lot and found this solution.

  1. use below line to print your Native lib path: System.out.println(System.getProperty("java.library.path"));
  2. if you are adding external openc4-version.jar then, in eclipse open BuildConfigurationPath->Library->opencv249.jar->Native Library and click edit and choose external folder which will be located inside /home/dav/Downloads/opencv-2.4.9/build/lib

Before this I built opencv as they metioned in java-opencv quickstart tutorial by creating build folder inside opencv unziped folder then ran 2 commands 1.cmake -DBUILD_SHARED_LIBS=OFF .. if you get error then check make installed or not 2.make -j8

Done.

saurabheights
  • 3,967
  • 2
  • 31
  • 50
DK Rajput
  • 1
  • 1