Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打包opencv+;jar中的java_Java_Opencv_Packaging_Executable Jar - Fatal编程技术网

如何打包opencv+;jar中的java

如何打包opencv+;jar中的java,java,opencv,packaging,executable-jar,Java,Opencv,Packaging,Executable Jar,我已经在Java中使用Opencv 2.4.5构建了一个应用程序,现在我想发布这个应用程序。使用以下命令加载库: static{ System.loadLibrary("opencv_java245"); } 这很好用。但是,在导出时,从jar运行时,它不起作用: java -jar build1.jar opencv_java245.jar文件作为一个用户库包含,并连接了一个本地文件(libopencv_java245.dylib)。当运行从Eclipse生成

我已经在Java中使用Opencv 2.4.5构建了一个应用程序,现在我想发布这个应用程序。使用以下命令加载库:

static{ 
        System.loadLibrary("opencv_java245"); 
    }
这很好用。但是,在导出时,从jar运行时,它不起作用:

java -jar build1.jar 
opencv_java245.jar文件作为一个用户库包含,并连接了一个本地文件(libopencv_java245.dylib)。当运行从Eclipse生成的可执行jar时,尽管在Eclipse中编译/运行得很好,但我得到了下面的unsatifiedLinkError

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)

看起来应该很好用。我使用了-D64和-D32进行测试,得到的结果与这两种方法相同

正如史蒂文·C所说,这就像是一场战争,也是一场战争。我对如何使用dylibs略知一二,并试图与使用“用户库”添加jar,然后添加本机dylib的方法保持一致。此外,出于某种原因,即使在使用“/”时加载资源也是从src目录加载的,而不是从我的项目的根目录加载的(我做的一个测试项目就是这样)

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

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加载本机库(或DLL)。有关如何处理此问题,请参阅链接问题。感谢@StephenC我尝试了此方法,但仍然收到不满意的LinkError消息(类似于未知类型)。完整消息是什么?没问题,已排序:)如果操作系统是Linux,该怎么办?看起来Linux使用了。所以文件
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);
    }