简体   繁体   English

带有OpenCV和Javafx的jar文件

[英]Jar file with openCV and javafx

I made a rubiks cube application which includes a cube detection system, As im getting closer to wrapping up the project I wanted to make a JAR file. 我制作了一个包括多维数据集检测系统的rubiks多维数据集应用程序,随着我接近完成项目的工作,我想制作一个JAR文件。 I get errors when running the JAR file and it really sucks because I can only run the project in IntelliJ(IDE). 运行JAR文件时出现错误,它确实很烂,因为我只能在IntelliJ(IDE)中运行项目。 I face two problems: 1) I cant get the JAR to link openCV properly 2)I have a problem loading an FXML file(this will occur assuming it did not crash while trying to load openCV. 我遇到两个问题:1)我无法正确连接openCV的JAR 2)加载FXML文件时遇到问题(假设在尝试加载openCV时没有崩溃,这会发生。

For case 1 here is the code and errors: 对于情况1,下面是代码和错误:

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

and the error message: 和错误消息:

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

Now If i remove the try catch comments i get this error: 现在,如果我删除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();
}

Error: 错误:

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

Here is my file structure: 这是我的文件结构: 在此处输入图片说明

and here is my artifact config: 这是我的工件配置: 在此处输入图片说明

Just go with the steps below: 只需执行以下步骤:

  1. Put the opencv_java342.dll in a folder let call it dll_libs in a drive eg C:\\dll_libs opencv_java342.dll放在一个文件夹中, dll_libs在驱动器中称为dll_libs ,例如C:\\dll_libs
  2. Then go to Environment Variable -> Edit path -> put C:\\dll_libs -> Apply -> OK 然后转到环境变量->编辑路径->放置C:\\dll_libs >应用->确定
  3. Restart your IDE 重新启动IDE

OR: Just put the opencv_java342.dll file in the C:\\Windows\\System32 folder 或:只需将opencv_java342.dll文件放在C:\\Windows\\System32文件夹中

And it is : ) 它是:)

Update (for your second question): If you want to give the executable package to someone else to run the program, there are almost 2 ways: 更新(针对第二个问题):如果要将可执行程序包交给其他人来运行程序,则有两种方法:

  1. Let them to create the environment manually, just as the answer to your question. 让他们手动创建环境,就像回答问题一样。
  2. Do the job programmatically, just within your code write a function which set the .dd environment variable on the program startup, and then load the lib. 以编程方式完成此工作,只需在您的代码内编写一个函数即可在程序启动时设置.dd环境变量,然后加载lib。

If you choose the second one, here I will give an example code to refere to: 如果您选择第二个,这里我将给出一个示例代码以供参考:

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

Hope it can help!! 希望能对您有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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