自己做的opencv for java 的javafx应用,在网上找了打包相关真是太费劲了。。记录一下以供自己之后用
首先打包相关配置:




现在打包相关配置已经完成了。
在编译器中运行时,我们可以在在Run/Debug Configurations中 VM options写入:
-Djava.library.path={dll文件位置}
同时在代码中写入
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
来调用dll文件。
但是在jar包运行的时候,会提示找不到dll,我们需要把jar包中的dll解压到一个缓存中,然后load,就要修改代码为下:
static {
try {
// getClass().getClassLoader().getResourceAsStream(arg0);
// System.out.println("MainTest.class.getClass()"+MainTest.class.getClass().toString());
InputStream in = Main.class
.getResourceAsStream("/resource/opencv_java410.dll"); //dll文件位置
System.out.println("Main.class.getClass()" + in);
File ffile = new File("");
String filePath = null;
filePath = ffile.getAbsolutePath() + File.separator
+ "opencv_java410.dll";
System.out.println("filePath opencv_java410.dll is " + filePath);
File dll = new File(filePath);
FileOutputStream out = new FileOutputStream(dll); //缓存dll位置
int i;
byte[] buf = new byte[1024];
try {
while ((i = in.read(buf)) != -1) {
out.write(buf, 0, i);
}
} finally {
in.close();
out.close();
}
System.load(dll.getAbsolutePath());//
dll.deleteOnExit();
} catch (Exception e) {
System.err.println("load jni error!");
}
}
另外,一定要注意在加载FXML文件时要用fxmlLoader.setLocation(getClass().getResource("/yixianyong/view/RootLayout.fxml"));或者类似的
而不是fxmlLoader.setLocation(getClass().getResource("…/view/RootLayout.fxml"));或者类似的
否则会出现错误:

最后打包:


build 或者rebuild都可以。
这篇博客记录了如何在IntelliJ IDEA中将一个使用javafx和opencv for java的项目,连同第三方库(dll)打包成jar。在运行时,需通过VM options指定dll路径,并使用System.loadLibrary加载。在处理FXML文件时,正确设置资源路径以避免错误。最终通过IDE的build功能完成打包。
1015

被折叠的 条评论
为什么被折叠?



