IntelliJ does not find native libraries for OpenCV when adding jar as a dependency for Play project

10,433

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

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

How to load native lib is following.

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

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

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.

Share:
10,433
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

    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

    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.

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

    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
    

    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
    

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

    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?

    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

    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