java 将 jar 添加为 Play 项目的依赖项时,IntelliJ 找不到 OpenCV 的本机库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17708150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ does not find native libraries for OpenCV when adding jar as a dependency for Play project
提问by Charles Bergmeier
I am currently working on a Play 2.1 project, in which requests to the web-service will handle downloading user-supplied images, re-sizing and re-cropping them, and also filtering out known bad photos (for example, we don't want users to upload company logos). We are trying to use OpenCV to handle the back-end work, but I can't seem to get IntelliJ to add the OpenCV jar in a way that works with the java project.
我目前正在处理一个 Play 2.1 项目,其中对网络服务的请求将处理下载用户提供的图像、重新调整大小和重新裁剪它们,并过滤掉已知的坏照片(例如,我们不希望用户上传公司徽标)。我们正在尝试使用 OpenCV 来处理后端工作,但我似乎无法让 IntelliJ 以适用于 java 项目的方式添加 OpenCV jar。
I've been able to build OpenCV from source, without issue. This left me with the following folder: /home/charles/opencv/release
我已经能够从源代码构建 OpenCV,没有问题。这给我留下了以下文件夹:/home/charles/opencv/release
Inside this folder, I have three files of interest:
在这个文件夹中,我有三个感兴趣的文件:
- bin/opencv-246.jar
- lib/cv2.so
- lib/libopencv_java246.so
- bin/opencv-246.jar
- 库/cv2.so
- lib/libopencv_java246.so
If I try to add the jar file to IntelliJ as a new Java library, it seemingly finds all the classes/methods, and I can write code using the auto-complete. I can also click on the respective classes or methods, and it brings me to the right files.
如果我尝试将 jar 文件作为新的 Java 库添加到 IntelliJ,它似乎可以找到所有类/方法,并且我可以使用自动完成功能编写代码。我还可以单击相应的类或方法,它会将我带到正确的文件。
However, when I try to run the Play project, I get this error:
但是,当我尝试运行 Play 项目时,出现此错误:
[info] Loading project definition from /home/charles/Github/ImageProject
[info] Set current project to ImageProject (in build file:/home/charles/Github/ImageProject/)
--- (Running the application from SBT, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
Server started, use Alt+D to stop
[info] Compiling 1 Java source to /home/charles/Github/ImageProject/target/scala-2.10/classes...
[error] /home/charles/Github/ImageProject/app/controllers/Application.java:7: error: package org.opencv.core does not exist
[error] import org.opencv.core.Core;
[error] ^
I've also tried adding a copy of the jar file directly into the project (so putting opencv-246.jar into ImageProject/lib), and then adding the java library from that location instead. But that just leaves me with a different error:
我还尝试将 jar 文件的副本直接添加到项目中(因此将 opencv-246.jar 放入 ImageProject/lib),然后从该位置添加 java 库。但这只会给我留下一个不同的错误:
java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path
I suspect part of the problem may be related to the native libraries that the Java OpenCV wrapper uses (file 2 or 3 above). In Eclipse, when you add a jar file, you can explicitly set the native library location, which makes OpenCV work fine. I've read suggestions of using this to fix the problem:
我怀疑部分问题可能与 Java OpenCV 包装器使用的本机库(上面的文件 2 或 3)有关。在 Eclipse 中,添加 jar 文件时,可以显式设置本机库位置,这使得 OpenCV 可以正常工作。我已经阅读了使用它来解决问题的建议:
-Djava.library.path=/home/charles/opencv/release/lib
But that doesn't seem to work (though maybe I'm setting it in the wrong place? I've tried setting it as a JVM parameter in the run config for the project, and in the IDE settings, but neither seem to be used or respected).
但这似乎不起作用(尽管我可能将其设置在错误的位置?我尝试在项目的运行配置和 IDE 设置中将其设置为 JVM 参数,但似乎都不是使用或尊重)。
Note: Just to clarify again, this is a Play2 project, not an Android project. There seems to be some Android-specific help out there, that isn't relevant in this case.
注意:再次澄清,这是一个 Play2 项目,而不是一个 Android 项目。似乎有一些特定于 Android 的帮助,在这种情况下无关紧要。
This feels like it should be a rather straight forward thing, but I've been spending several days trying to find an answer at this point, and still have nothing. Any ideas?
感觉这应该是一件相当直接的事情,但我已经花了几天时间试图在这一点上找到答案,但仍然一无所获。有任何想法吗?
Additional details:I also tried following the "Running SBT samples" of the OpenCV documentation here: http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html
其他详细信息:我还尝试在此处遵循 OpenCV 文档的“运行 SBT 示例”:http: //docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html
And I also get a similar error:
我也收到类似的错误:
charles@charles-VirtualBox:~/JavaSample$ sbt run
[info] Loading project definition from /home/charles/JavaSample/project
[info] Set current project to JavaSample (in build file:/home/charles/JavaSample/)
[info] Compiling 1 Java source to /home/charles/JavaSample/target/scala-2.10/classes...
[info] Running HelloOpenCV
Hello, OpenCV
[error] (run-main) java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path
java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at HelloOpenCV.main(HelloOpenCV.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 2 s, completed Jul 17, 2013 5:11:39 PM
采纳答案by Yoonje Choi
When you want to use OpenCV or any other native libraries on Playframework, You MUST run your application with "play start" command, not "play run".
当您想在 Playframework 上使用 OpenCV 或任何其他本机库时,您必须使用“播放开始”命令而不是“播放运行”来运行您的应用程序。
"play run" command starts your application in development mode and "play start" command starts in production mode. I don't know every difference between them but one obvious thing is ,
“play run”命令在开发模式下启动你的应用程序,而“play start”命令在生产模式下启动。我不知道它们之间的每一个区别,但很明显的一件事是,
Only when we use "play start", a new JVM for you application is launched and it loads native libraries you specified by System.load("/absolute/path/to/your/so/or/jnilib/inOSX/not/dylib/filename.jnilib");
只有当我们使用“播放开始”时,才会为您的应用程序启动一个新的 JVM 并加载您由 System.load("/absolute/path/to/your/so/or/jnilib/inOSX/not/dylib /filename.jnilib");
How to load native lib is following.
如何加载本机库如下。
Create Global.java which has empty package name. (refer this link)
创建具有空包名称的 Global.java。(参考这个链接)
public class Global extends GlobalSettings {
@Override
public void beforeStart(Application app) {
// TODO Auto-generated method stub
super.beforeStart(app);
String libopencv_java = "/Users/yoonjechoi/git/myFirstApp/target/native_libraries/64bits/libopencv_java246.jnilib";
System.load(libopencv_java);
}
}
then you can use classes of OpenCV in your Play application's controllers.
然后您可以在 Play 应用程序的控制器中使用 OpenCV 类。
System.loadLibrary("opencv_java246") doesn't works. I don't know why. I don't have time to dig why. -_-;
System.loadLibrary("opencv_java246") 不起作用。我不知道为什么。我没有时间去挖掘原因。-_-;
Please give hints if you know why.
如果你知道为什么,请给出提示。
回答by user1114134
for windows 64 bit set
对于 Windows 64 位集
-Djava.library.path=/full-path/OpenCV/build/java/x64
-Djava.library.path=/full-path/OpenCV/build/java/x64
回答by Damilola
I would have used the lib/libopencv_java246.so
我会使用 lib/libopencv_java246.so
It's always important to map the .so file to build library. It's suppose to work out of the box
将 .so 文件映射到构建库总是很重要的。它应该是开箱即用的