[英]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.
Just go with the steps below: 只需执行以下步骤:
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
C:\\dll_libs
-> Apply -> OK C:\\dll_libs
>应用->确定 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: 更新(针对第二个问题):如果要将可执行程序包交给其他人来运行程序,则有两种方法:
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.