Wednesday, March 8, 2017

How to add OpenCV lib to Dynamic Web Project

Leave a Comment

Currently, I am building a Java web project that use Opencv to detect images that are similar. But when I run, I always get this error

java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: opencv_java249 java.lang.Runtime.load0(Runtime.java:806) java.lang.System.load(System.java:1086) com.hadoop.DriverServlet.doPost(DriverServlet.java:25) javax.servlet.http.HttpServlet.service(HttpServlet.java:650) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I also search this problem but still can not find any solutions for my case. even I try this http://examples.javacodegeeks.com/java-basics/java-library-path-what-is-it-and-how-to-use/ to add to java.library path point to opencv-249 jar in eclipse but still not be resolved.

Anyone can help me? Thanks in advance.

2 Answers

Answers 1

To work with opencv you need jar file and binary file. JAR file can be simply added by local maven repository or any other variant.

Binary file you need to add and load manually. Something like this:

private static void addLibraryPath(String pathToAdd) throws Exception{     final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");     usrPathsField.setAccessible(true);      //get array of paths     final String[] paths = (String[])usrPathsField.get(null);      //check if the path to add is already present     for(String path : paths) {         if(path.equals(pathToAdd)) {             return;         }     }      //add the new path     final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);     newPaths[newPaths.length-1] = pathToAdd;     usrPathsField.set(null, newPaths); }  public void init() {         String pathToOpenCvDll = "c:\\opencv\\"; //linux path works too         try {             addLibraryPath(pathToOpenCvDll);             System.loadLibrary("opencv_java320");         } catch (Exception ignored) {         }     } } 

Answers 2

For web project, the lib jar file should be in the WEB-INF/lib dir.

Also make sure the jars in the dir are in the classpath

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment