如何在jar中打包opencv + java [英] How to package opencv +java in a jar

查看:947
本文介绍了如何在jar中打包opencv + java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Opencv 2.4.5和Java构建一个应用程序,现在想要分发应用程序。使用以下内容加载库:

I've been using Opencv 2.4.5 with Java for a while building an application and would now like to distribute the app. The library is loaded using the following:

static{ 
        System.loadLibrary("opencv_java245"); 
    }

工作正常。但是,在导出时,从jar运行时它不起作用:

which works fine. However, when exporting, it doesn't work when running from the jar:

java -jar build1.jar 

opencv_java245.jar文件作为用户库包含在内,并连接了一个本机文件(libopencv_java245.dylib)。当运行从Eclipse生成的可执行jar时,我得到下面的UnsatisfiedLinkError,尽管在eclipse中编译/运行正常。

The opencv_java245.jar file is included as a user library, with a native file (libopencv_java245.dylib) connected to it. When running the executable jar generated from Eclipse, I get the UnsatisfiedLinkError below, despite things compiling/running fine in eclipse.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java245 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.drawbridge.Main.<clinit>(Main.java:12)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:266)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)

任何人都知道在罐子里打包OpenCV的简单方法吗?

Anyone know a simple way of packaging OpenCV in the jar?

更新:我现在已经筋疲力尽了。我可以将库添加到我的构建路径(而不是使用System.loadLibrary),这可以在eclipse中工作,但不能在jar中打包。我已经尝试了一切。我还检查了我正在尝试加载的动态库的类型 - 它是

Update: I have now exhausted everything. I can add the library to my build path (and not use System.loadLibrary) and that works in eclipse, but not when packaged in the jar. I've tried everything. I also checked the type of dynamic library I'm trying to load - it's

Mach-O 64-bit x86_64 dynamically linked shared library

看起来它应该可以正常工作。我用-D64和-D32来测试并得到两者相同的结果。

which seems like it should work fine. I've used -D64 and -D32 to test and get the same result with both.

推荐答案

正如Steven C所说,它就像从JAR中提取和加载DLL 以及错误报告。我对如何使用dylib略显无知,并试图与 OpenCV教程,它使用用户库添加jar,然后添加本机dylib。此外,由于某些原因加载资源,即使使用/是从src目录加载,而不是我的项目的根目录(在我做的测试项目中就是这种情况)。

As Steven C said, it was as in Extract and load DLL from JAR and also in a bug report. I was slightly ignorant of how to use dylibs and was trying to be consistent with an OpenCV tutorial which used a "user library" to add a jar, and then add the native dylib. Also, for some reason loading resources even when using "/" was loading from the src directory, not my project's root directory (which was the case in a test project I made).

对于那些尝试做同样事情的人,这里有一些代码可以提供帮助:

For those trying to do the same thing, here's some code to help:

private static void loadLibrary() {
    try {
        InputStream in = null;
        File fileOut = null;
        String osName = System.getProperty("os.name");
        Utils.out.println(Main.class, osName);
        if(osName.startsWith("Windows")){
            int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
            if(bitness == 32){
                Utils.out.println(Main.class, "32 bit detected");
                in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
                fileOut = File.createTempFile("lib", ".dll");
            }
            else if (bitness == 64){
                Utils.out.println(Main.class, "64 bit detected");
                in = Main.class.getResourceAsStream("/opencv/x64/opencv_java245.dll");
                fileOut = File.createTempFile("lib", ".dll");
            }
            else{
                Utils.out.println(Main.class, "Unknown bit detected - trying with 32 bit");
                in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
                fileOut = File.createTempFile("lib", ".dll");
            }
        }
        else if(osName.equals("Mac OS X")){
            in = Main.class.getResourceAsStream("/opencv/mac/libopencv_java245.dylib");
            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) {
        throw new RuntimeException("Failed to load opencv native library", e);
    }

这篇关于如何在jar中打包opencv + java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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