Ask Your Question

OpenCV not loaded in Tomcat

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

kakin83 gravatar image

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

berak gravatar image

hi all,

I have followed the following page

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

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!


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

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

Andrey gravatar image

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

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>(

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

OpenCV 248

edit flag offensive delete link more

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

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


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

Seen: 2,056 times

Last updated: Mar 19 '14