gpt4 book ai didi

java - 带有 openCV 和 javafx 的 Jar 文件

转载 作者:行者123 更新时间:2023-12-02 10:43:27 24 4
gpt4 key购买 nike

我制作了一个 rubiks 立方体应用程序,其中包括一个立方体检测系统,当我接近完成该项目时,我想制作一个 JAR 文件。运行 JAR 文件时出现错误,这真的很糟糕,因为我只能在 IntelliJ(IDE) 中运行该项目。我面临两个问题:1)我无法让 JAR 正确链接 openCV2)我在加载 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. opencv_java342.dll放入一个文件夹中,并在一个文件夹中将其命名为dll_libs驱动器,例如 C:\dll_libs
  2. 然后转到环境变量 -> 编辑路径 -> 将 C:\dll_libs ->应用->确定
  3. 重新启动您的 IDE

或者:只需将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);
}

希望对你有帮助!!

关于java - 带有 openCV 和 javafx 的 Jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52771617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com