简体   繁体   中英

Create a jar file including an external library (OpenCV) in eclipse

I'm trying to create an executable jar of my application on a Mac, which uses functions from OpenCV. However when I try to create the jar in eclipse I get this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java248 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at imageRegistration.ImageGUI.main(ImageGUI.java:643)

The error I think is because the there is a .dylib file that is linked to the OpenCV jar and is not being packaged with it.

I have found this , which seems to be the same issue I am having, but the solution doesn't work for me. This is what I have done:

public static void libLoad(){
    try{
    InputStream in = ImageGUI.class.getResourceAsStream("/lib/opencv-2.4.8 MAC/build/lib/libopencv_java248.dylib");
    File fileOut = File.createTempFile("lib", ".dylib");

    OutputStream out = FileUtils.openOutputStream(fileOut);
    IOUtils.copy(in, out);
    in.close();
    out.close();
    System.load(fileOut.toString());
    } catch(Exception e) {
        System.out.println("Failed to load opencv native library \n" + e);
    }
}

The error I get when I run this is:

Failed to load opencv native library 
java.lang.NullPointerException

EDIT: Here is the full stack trace:

java.lang.NullPointerException
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
    at imageRegistration.ImageGUI.libLoad(ImageGUI.java:624)
    at imageRegistration.ImageGUI.main(ImageGUI.java:643)

You need to print full stack trace, it's not clear from where NPE is coming. Most probably ImageGUI.class.getResourceAsStream returns null. If this is true it is eihter because path is wrong or lib is not in the jar

You can try to put native OpenCV DLL file under the bin directory of JRE. At least it worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM