ali*_*air 8 java opencv packaging executable-jar
我一直在使用Opencv 2.4.5和Java构建一个应用程序,现在想分发应用程序.使用以下内容加载库:
static{
System.loadLibrary("opencv_java245");
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.但是,在导出时,从jar运行时它不起作用:
java -jar build1.jar
Run Code Online (Sandbox Code Playgroud)
opencv_java245.jar文件作为用户库包含在内,并连接了一个本机文件(libopencv_java245.dylib).当运行从Eclipse生成的可执行jar时,我得到下面的UnsatisfiedLinkError,尽管在eclipse中编译/运行正常.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java245 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at com.drawbridge.Main.<clinit>(Main.java:12)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:266)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Run Code Online (Sandbox Code Playgroud)
有人知道在罐子里打包OpenCV的简单方法吗?
更新:我现在已经筋疲力尽了.我可以将库添加到我的构建路径(而不是使用System.loadLibrary),这可以在eclipse中使用,但不能在jar中打包.我已经尝试了一切.我还检查了我正在尝试加载的动态库的类型 - 它是
Mach-O 64-bit x86_64 dynamically linked shared library
Run Code Online (Sandbox Code Playgroud)
这似乎应该工作正常.我用-D64和-D32来测试并得到两者相同的结果.
ali*_*air 11
正如Steven C所说,它就像在Extract中一样,从JAR加载DLL,也在bug报告中.我对如何使用dylib略显无知,并且试图与使用"用户库"添加jar 的OpenCV教程保持一致,然后添加本机dylib.此外,由于某些原因加载资源,即使使用"/"从src目录加载,而不是我的项目的根目录(在我做的测试项目中就是这种情况).
对于那些试图做同样事情的人,这里有一些代码可以帮助:
private static void loadLibrary() {
try {
InputStream in = null;
File fileOut = null;
String osName = System.getProperty("os.name");
Utils.out.println(Main.class, osName);
if(osName.startsWith("Windows")){
int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if(bitness == 32){
Utils.out.println(Main.class, "32 bit detected");
in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
else if (bitness == 64){
Utils.out.println(Main.class, "64 bit detected");
in = Main.class.getResourceAsStream("/opencv/x64/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
else{
Utils.out.println(Main.class, "Unknown bit detected - trying with 32 bit");
in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
}
else if(osName.equals("Mac OS X")){
in = Main.class.getResourceAsStream("/opencv/mac/libopencv_java245.dylib");
fileOut = File.createTempFile("lib", ".dylib");
}
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();
System.load(fileOut.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to load opencv native library", e);
}
Run Code Online (Sandbox Code Playgroud)