Tomcat中的OpenCV出现UnsatisfiedLinkError [英] UnsatisfiedLinkError with OpenCV in Tomcat
问题描述
首先,我有一个在主要方法中运行的OpenCV的基本示例. 但是,如果我在Spring Web Controller中使用OpenCV,则会引发错误.
First of all, I have a basic example of OpenCV running in a main method. However, if I use OpenCV in a Spring Web Controller, an error is thrown.
在STS(Spring Tool Suite)的 Tomcat 服务器中运行以下代码时,出现UnsatisfiedLinkError异常,并将问题归结为以下代码:
I am getting an UnsatisfiedLinkError when running the following code inside a Tomcat Server, within STS (Spring Tool Suite), and have boiled the problem down to the following code:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier cascadeClassifier = new CascadeClassifier();
服务器和主"应用程序的运行配置都包含指向OpenCV DLL的VM参数:
The run configurations of both the server and the "main" application contain the VM Arguments pointing to the OpenCV DLLs:
-Djava.library.path="C:\opencv\build\java\x64;C:\opencv\build\x64\vc10\bin"
作为参考,我还包括了以下堆栈跟踪的相关位:
For reference, I have also included the pertinent bits of the stacktrace below:
org.springframework.web.util.NestedServletException: Handler processing failed;
nested exception is
java.lang.UnsatisfiedLinkError: org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0()J
...
Caused by: java.lang.UnsatisfiedLinkError:
org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0()J
at org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0(Native Method)
at org.opencv.objdetect.CascadeClassifier.<init>(CascadeClassifier.java:38)
at com.immersion.test.controllers.SimpleController.createClassifier(SimpleController.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
注意:我已经阅读了几个 StackOverflow线程,其中缺少UnsatisfiedLinkError的主要原因System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
呼叫.显然这里不是这种情况,因为它是在引发异常的上方调用的.
Note: I have read in several StackOverflow threads that the main cause of the UnsatisfiedLinkError is the missing System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
call. This is clearly not the case here, since it is being called just above where the exception is thrown.
推荐答案
事实证明,用于在STS/Eclipse中启动Tomcat服务器的默认运行配置实际上只是Tomcat的启动器. 这意味着添加到Tomcat运行配置"的任何VM Args都不会传输到实际的Tomcat实例.
It turns out that the default run configuration to start a Tomcat server within STS/Eclipse is essentially just a launcher for Tomcat. This means that any VM Args that are added to the Tomcat 'run configuration' will not be transferred to the actual Tomcat instance.
在Tomcat目录中的tcruntime-instance.bat
中查找(这最终创建了tomcat Java实例),我们可以看到,只需将我们想要的VM Args添加到JAVA_OPTS
环境变量中,这些dll就可以使用了.到Tomcat.
On looking in tcruntime-instance.bat
within the Tomcat directory (this is what ends up creating the tomcat java instance), we can see that simply by adding our intended VM Args to the JAVA_OPTS
environment variable, the dlls will be available to Tomcat.
TL; DR:
所以.只需将OpenCV二进制文件的位置添加到您的JAVA_OPTS
环境变量中:
So.. Simply add the the locations of the OpenCV binaries to your JAVA_OPTS
environment variable:
-Djava.library.path="C:\opencv\build\java\x64;C:\opencv\build\x64\vc10\bin"
有关为Tomcat设置JAVA_OPTS的更多信息,请参见此这样的问题.
For more information on setting JAVA_OPTS for Tomcat, see this SO question.
这篇关于Tomcat中的OpenCV出现UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!