带有OpenCV和Javafx的jar文件

时间:2018-10-12 03:04:39

标签: java opencv

我制作了一个包括多维数据集检测系统的rubiks多维数据集应用程序,随着我接近完成项目的工作,我想制作一个JAR文件。运行JAR文件时出现错误,它确实很烂,因为我只能在IntelliJ(IDE)中运行项目。 我面临两个问题: 1)我无法让JAR正确链接openCV 2)我在加载FXML文件时遇到问题(如果尝试加载openCV时没有崩溃,就会发生这种情况。

情况1的代码和错误如下:

static {
   // try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 //   } catch (UnsatisfiedLinkError e) {
   //     System.err.println("Could not find OpenCV Library!");
 //   }
}

和错误消息:

Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java342 in java.library.path:

现在,如果我删除try catch注释,则会出现此错误:

static {
    try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    } catch (UnsatisfiedLinkError e) {
       System.err.println("Could not find OpenCV Library!");
    }
}

@Override
public void start(Stage primaryStage) throws Exception {
    //Loader
    FXMLLoader loader = new FXMLLoader(main.class.getResource("FXML_layouts\\MainScreen.fxml"));

    Scene scene = new Scene(loader.load());
    ((mainController)loader.getController()).setStage(primaryStage);

    primaryStage.setScene(scene);
    primaryStage.show();
}

错误:

    Could not find OpenCV Library!
Exception in Application start method
java.lang.reflect.InvocationTargetException
....
Caused by: java.lang.IllegalStateException: Location is not set.

这是我的文件结构: enter image description here

这是我的工件配置: enter image description here

1 个答案:

答案 0 :(得分:1)

只需执行以下步骤:

  1. opencv_java342.dll放在一个文件夹中,让它dll_libs放在一个文件夹中 驱动器,例如C:\dll_libs
  2. 然后转到环境变量->编辑路径->放置C:\dll_libs-> 申请->确定
  3. 重新启动您的IDE

OR::只需将opencv_java342.dll文件放在C:\Windows\System32文件夹中

它是:)

更新(第二个问题):如果您想将可执行程序包交给其他人来运行程序,则有两种方法:

  1. 让他们手动创建环境,就像答案 您的问题。
  2. 以编程方式完成工作,只需在代码内编写一个函数 在程序启动时设置.dd环境变量,以及 然后加载库。

如果您选择第二个,这里我将给出一个示例代码供您参考:

public static void loadOpenCVLib() throws Exception {
    File file = new File(OpenCVUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    File opencv_libs = new File("oc_lib"); // this path is where is the lib going to copy to
    String model = System.getProperty("sun.arch.data.model");
    String localLibPath; // this is the path inside your program resource
    if (model.equals("64")) {
        localLibPath = "oc_lib/64bit";
    } else {
        localLibPath = "oc_lib/x86";
    }
    if (file.isFile()) { // when run from jar
        JarFile jar = new JarFile(file);
        if (!opencv_libs.exists() || !opencv_libs.isDirectory()) {
            try {
                JarUtils.copyResourcesToDirectory(jar, localLibPath, opencv_libs.getAbsolutePath());
            } catch (Exception e) {
                throw new IOException("Failed to create load opencv libs!!");
            }
        } else {
            String[] list = opencv_libs.list();
            if (list != null && list.length != 2) {
                try {
                    JarUtils.copyResourcesToDirectory(jar, localLibPath, opencv_libs.getAbsolutePath());
                } catch (Exception e) {
                    throw new IOException("Failed to create load opencv libs!!");
                }
            }
        }
    } else { // when run from IDE
        File libPath = new File(OpenCVUtil.class.getResource("/"+localLibPath).getFile());
        if (!opencv_libs.exists() || !opencv_libs.isDirectory()) {
            boolean isDone = opencv_libs.mkdir();
            if (!isDone && !opencv_libs.exists()) {
                throw new IOException("Failed to create load opencv libs!!");
            }
            try {
                FileUtils.copyDirectory(libPath, opencv_libs);
            } catch (IOException e) {
                throw new IOException("Failed to create load opencv libs!!");
            }
        } else {
            String[] list1 = opencv_libs.list();
            String[] list2 = libPath.list();
            boolean contentEquals = list1 != null && list2 != null && list1.length == list2.length;
            if (contentEquals) {
                try {
                    FileUtils.copyDirectory(libPath, opencv_libs);
                } catch (IOException e) {
                    throw new IOException("Failed to create load opencv libs!!");
                }
            }
        }
    }
    System.setProperty("java.library.path", opencv_libs.getAbsolutePath());
    Field sys_paths = ClassLoader.class.getDeclaredField("sys_paths");
    sys_paths.setAccessible(true);
    sys_paths.set(null, null);
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    // it is for the ffmpeg name
    String[] list = opencv_libs.list();
    assert list != null;
    String ffmpeg_dll_file_name = null;
    for (String s : list) {
        if (s.contains("ffmpeg")) {
            ffmpeg_dll_file_name = s.substring(0, s.indexOf("."));
        }
    }
    System.loadLibrary(ffmpeg_dll_file_name);
}

希望它可以帮助您!