Ask Your Question
1

OpenCV not loaded in Tomcat

asked 2013-06-05 21:53:32 -0600

kakin83 gravatar image

updated 2014-03-19 16:04:13 -0600

berak gravatar image

hi all,

I have followed the following page

http://docs.opencv.org/2.4.4-beta/doc/tutorials/introduction/desktop_java/java_dev_intro.html

to create a simple project in eclipse, and it works (with jre7, opencv2.4.5)

import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Main {
    public static void main(String[] args) {
        System.loadLibrary("opencv_java245");
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("m = " + m.dump());
    }
}

however, when i paste the same code in a helloworld servlet in my tomcat (tomcat 6.0.37, same jre, x64) as a war, it does not work with the UnsatisifiedLinkError for the n_eye component at Mat.eye. i hv managed to move the loadLibrary statement to a static block in a separate class

public class OpenCVLoader {
    static {
        System.loadLibrary("opencv_java245");
    }
...
}

The OpenCVLoader class is packed in a jar placed under $(catalina.home}/lib to be loaded on startup automatically. i hv checked in my helloworld servlet that the opencv_java245.dll has successfully loaded into the memory. (i.e. use reflection to list all the loaded library)

it seems that the dll may not be correctly loaded or linked even it is claimed loaded in tomcat. could anyone give me some help?

thanks a lot!

Carlos

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2014-03-19 15:56:15 -0600

Andrey gravatar image

updated 2014-03-19 15:57:34 -0600

Similar problem

java.lang.UnsatisfiedLinkError: org.opencv.highgui.VideoCapture.VideoCapture_2(I)J at org.opencv.highgui.VideoCapture.VideoCapture_2(Native Method) at org.opencv.highgui.VideoCapture.<init>(VideoCapture.java:154)

It works as Console App, but not works from the servlet (Tomcat)

OpenCV 248

edit flag offensive delete link more
0

answered 2015-03-18 16:45:42 -0600

Carlos, did you find the way to get through the issue? I had same problem, and could solve it fortunately. Here's my guessing and solution, though I'm not an expert of Tomcat and not pretty sure if this guessing is true.

Tomcat has several classloaders, as you probably know. Because JavaVM can load a native library only once, it probably is a good practice to load OpenCV's native library with 'Common' class loader, just like the way you did by putting your OpenCVLoader in $(catalina.home}/lib.

Here, I guess that you might haven't loaded opencv*.jar with same class loader. When I made many try-and-errors to make my servlet work on Tomcat, I finally found putting both of a small loader class (just like yours!) and opencv itself in the $(catalina.home)/lib can solve the UnsatisfiedLinkError problem. I think a class that is loaded by WebApp class loader cannot access a native module that is loaded by Common class loader.

I believe dev team of opencv's java binding should add a loader class (or method) to official opencv releases, to save many servlet developers working with Tomcat.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-05 21:53:32 -0600

Seen: 2,210 times

Last updated: Mar 19 '14