当添加jar作为Play项目的依赖项时,IntelliJ找不到OpenCV的本机库 [英] IntelliJ does not find native libraries for OpenCV when adding jar as a dependency for Play project

查看:355
本文介绍了当添加jar作为Play项目的依赖项时,IntelliJ找不到OpenCV的本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Play 2.1项目,其中对Web服务的请求将处理下载用户提供的图像,重新调整大小和重新裁剪它们,以及过滤掉已知的坏照片(例如,我们不希望用户上传公司徽标)。我们正在尝试使用OpenCV来处理后端工作,但我似乎无法让IntelliJ以一种适用于java项目的方式添加OpenCV jar。

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.

我已经能够从源代码构建OpenCV,没有问题。这给我留下了以下文件夹:
/ home / charles / opencv / release

I've been able to build OpenCV from source, without issue. This left me with the following folder: /home/charles/opencv/release

在这个文件夹中,我有三个感兴趣的文件:

Inside this folder, I have three files of interest:


  1. bin / opencv-246.jar

  2. lib / cv2.so

  3. lib / libopencv_java246.so

如果我尝试将jar文件作为新的Java库添加到IntelliJ,它似乎找到所有类/方法,我可以使用自动完成编写代码。我也可以单击相应的类或方法,它会将我带到正确的文件。

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.

然而,当我尝试运行Play项目时,我收到此错误:

However, when I try to run the Play project, I get this error:

[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]                       ^

我也试过直接添加jar文件的副本进入项目(所以将opencv-246.jar放入ImageProject / lib),然后从该位置添加java库。但这只是让我有一个不同的错误:

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:

java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path

我怀疑部分问题可能与Java OpenCV包装器使用的本机库有关(文件2或以上3)。在Eclipse中,当您添加jar文件时,您可以显式设置本机库位置,这使OpenCV正常工作。我已经阅读了使用它来解决问题的建议:

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:

-Djava.library.path=/home/charles/opencv/release/lib

但这似乎不起作用(尽管我可能正在设置它错误的地方?我已经尝试在项目的运行配置中将其设置为JVM参数,并且在IDE设置中,但似乎都没有使用或被尊重)。

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

注意:再说一遍,这是一个Play2项目,而不是Android项目。似乎有一些特定于Android的帮助,在这种情况下是不相关的。

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.

这感觉它应该是一个相当直接的东西,但我在这一点上已经花了好几天试图找到答案,但仍然没有任何东西。有什么想法吗?

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?

其他详细信息:
我也尝试过这里的OpenCV文档的运行SBT示例:
http://docs.opencv.org/doc/tutorials/introduction /desktop_java/java_dev_intro.html

我也收到类似的错误:

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


推荐答案

当您想在Playframework上使用OpenCV或任何其他本机库时,您必须运行您的应用程序play start命令,而不是play run。

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".

play run命令在开发模式下启动应用程序,play start命令在生产模式下启动。我不知道它们之间的每一个区别,但一个显而易见的事情是,

"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 start时,才会启动一个适用于您的应用程序的新JVM并加载它您通过System.load指定的本机库(/ absolute / path / to / your / so /或/ jnilib / inOSX / not / dylib / filename.jnilib);

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");

如何加载本机库如下。

创建具有空包名称的Global.java。 (请参阅此链接

Create Global.java which has empty package name. (refer this link )

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);
    }
}

然后你可以在Play应用程序中使用OpenCV类控制器。

then you can use classes of OpenCV in your Play application's controllers.

System.loadLibrary(opencv_java246)不起作用。我不知道为什么。我没时间挖掘原因。 -_-;

System.loadLibrary("opencv_java246") doesn't works. I don't know why. I don't have time to dig why. -_-;

如果您知道原因,请提供提示。

Please give hints if you know why.

这篇关于当添加jar作为Play项目的依赖项时,IntelliJ找不到OpenCV的本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆