Ask Your Question
4

JNI UnsatisfiedLinkError when running ant sample on Ubuntu

asked 2013-06-06 22:41:33 -0600

Pete Warden gravatar image

I've downloaded and built OpenCV top-of-tree 2.4.9 on a stock Ubuntu 12.04 machine, but the official samples/java/ant example dies with the following error when I follow the tutorial instructions:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIIDDDD)J

I've determined that the library exists and is being loaded through System.loadLibrary(). I've also used nm to examine libopencv_java249.so and I see the following entry for the function:

00000000000fe510 t Java_org_opencv_core_Mat_n_1Mat__IIIDDDD

I've also built a simple standalone JNI project with no OpenCV dependencies to ensure that there's not a wider Java problem on my system, but that works as expected. I've also run the exact same source tree successfully on my OS X system.

I'm at my wits end on this - does anybody have any ideas on how I can debug this further? It feels like there's a linking or symbol decoration problem, but I can't see what. More system details below:

Linux lucid64 2.6.32-42-server #96-Ubuntu SMP Wed Aug 15 19:52:20 UTC 2012 x86_64 GNU/Linux

java version "1.6.0_27"

OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.10.04.1)

OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
2

answered 2013-06-08 02:08:59 -0600

Andrey Pavlenko gravatar image

This is a bug, could you submit a ticket to OpenCV bug tracker? And I suggest you use stable '2.4' branch, because 'master' currently contains next major version development code.

edit flag offensive delete link more
0

answered 2013-06-13 05:45:06 -0600

ggl gravatar image

updated 2013-06-14 09:03:57 -0600

Andrey Pavlenko gravatar image

I get this error ONLY when I try to run as a groovy script, but not when I use the java classes. I use OpenCV 2.4.5 (installed via git as suggested in the intro to Java development OpenCV 2.4.5 document). Instead of using the ant build, I simply compile and then run SimpleSample.java which works fine, as in:

javac -cp ../build/bin/opencv-245.jar:. SimpleSample.java
java -cp ../build/bin/opencv-245.jar:. -Djava.library.path=../build/lib SimpleSample

My goal is to run this in groovy. I have groovy-fied SimpleSample.java as follows:

import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.CvType
import org.opencv.core.Scalar

System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
println("Welcome to OpenCV " + Core.VERSION)
def m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0)) 
println("OpenCV Mat: " + m)
...

I compile this with groovyc and then run it with java, which throws the exception.

groovyc -cp ../build/bin/opencv-245.jar:. TestGroovy.groovy
java -cp ../build/bin/opencv-245.jar:/usr/local/groovy/embeddable/groovy-all-2.1.4.jar:. -Djava.library.path=../build/lib TestGroovy

Welcome to OpenCV 2.4.5.0
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIIDDDD)J
edit flag offensive delete link more
0

answered 2013-07-16 08:11:15 -0600

Yoonje Choi gravatar image

Dear guys

I got very similar problem with play framework. I posted this, http://answers.opencv.org/question/16689/jni-error-on-playframework-v211/ .

ggl, was it a classloader related problem? In my case, I hardly find a clue where should I start to solve because the other jni libraries works well but opencv doesn't.

symbol table problem with compilation or classloader related problem?

Thanks in advance..

edit flag offensive delete link more
0

answered 2013-06-13 10:46:18 -0600

ggl gravatar image

After a bit of extra work, I got the groovy problem solved as well! For some reason the System.loadLibrary(CORE.NATIVE_LIBRARY_NAME) does not work well with Groovy.

I wrote the following (somewhat trivial) Java class:

import org.opencv.core.Core;
public class LibLoader {
  public static void load() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  }
}

Compile with:

javac -cp ../build/bin/opencv-245.jar:. LibLoader.java
In the groovy script use the static class as follows:
LibLoader.load()
I still have to pre-compile first with groovyc and then run the compiled java class. Here is the DetectFaceDemo as a groovy script:
/*
 * Detects faces in an image, draws boxes around them, and writes the results
 * to "faceDetection.png".
 */

LibLoader.load()

println("\nRunning DetectFaceDemo")

// Create a face detector from the cascade file in the resources directory.
// Note: I got rid of the getClass.getResource.getPath construction (lazy)
def faceDetector = new CascadeClassifier("resources/lbpcascade_frontalface.xml")
def image = Highgui.imread("resources/AverageMaleFace.jpg")

// Detect faces in the image.
// MatOfRect is a special container class for Rect.
def faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)

println(String.format("Detected %s faces", faceDetections.toArray().length))

// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
  Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
      + rect.width, rect.y + rect.height), new Scalar(0, 255, 0))
}

// Save the visualized detection.
def filename = "faceDetection.png"
println(String.format("Writing %s", filename))
Highgui.imwrite(filename, image)

Compile and run with:

groovyc -cp ../build/bin/opencv-245.jar:. DetectFace.groovy
java -cp ../build/bin/opencv-245.jar:/usr/local/groovy/embeddable/groovy-all-2.1.4.jar:. -Djava.library.path=../build/lib DetectFace

Results:

Running DetectFaceDemo
Detected 1 faces
Writing faceDetection.png

I reckon I will get groovy to work without the need for compilation first (there seems to be a problem with java.library.path).

What's neat about groovy is the simplicity of the code. I used to work with Java Advanced Imaging, but now that OpenCV has gpu-enabled routines, I want to go this road.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-06 22:41:33 -0600

Seen: 4,297 times

Last updated: Jul 16 '13