How to pack opencv + java in a jar

I have been using Opencv 2.4.5 with Java for some time creating an application, and now I would like to distribute the application. The library is loaded as follows:

static{ System.loadLibrary("opencv_java245"); } 

which works great. However, when exporting, it does not work when starting from a jar:

 java -jar build1.jar 

The opencv_java245.jar file is included as a user library to which the native file is connected (libopencv_java245.dylib). When I run an executable jar generated from Eclipse, I get an UnsatisfiedLinkError below, even though the compilation / work is doing fine in 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) 

Does anyone know a simple way to pack OpenCV in a jar?

Update: now I have exhausted everything. I can add a library to my build path (and not use System.loadLibrary) and it works in eclipse, but not when packing in a jar. I tried everything. I also checked the type of dynamic library I'm trying to load - it

 Mach-O 64-bit x86_64 dynamically linked shared library 

which seems to be working fine. I used -D64 and -D32 to test and get the same result with both.

+8
java executable-jar opencv packaging
source share
1 answer

As Stephen S said, it was both in Extract and load DLLs from the JAR , as well as in the error report . I was a little unaware of how to use dylib, and tried to be compatible with the OpenCV tutorial , which used a "user library" to add a jar, and then add a native dylib. In addition, for some reason, loading resources even when using "/" was loaded from the src directory, and not into the project root directory (which was the case with the test project I did).

For those trying to do the same, here is the code:

 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); } 
+10
source share

All Articles