0

I'm trying to set up openCV for a spring rest API, running on TOMCAT 8.5 server.

I tried several things before asking the question :

  • I added the opencv to the buildpath
  • I added the opencv library to src/WEB-INF/lib of my java project (which I had to create by myself)
  • I tried to use a maven repository : OpenPnp

    org.openpnp opencv 3.2.0-0

My test code is the following one : @RestController public class OpencvController {

    @GetMapping("/opencv")
    public  String opencv() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("mat = " + mat.dump());
        return null;
    }

}

But i keep getting the error : "java.lang.NoClassDefFoundError: org/opencv/core/Core" and i don't have anymore clue on how to solve it, any help would be really appreciated. Thank you in advance

Alexis Brunet
  • 335
  • 5
  • 15
  • Check if the WAR after package contain web-inf/lib/opencv.jar ? – Ken Chan Dec 29 '18 at 14:30
  • I just cheked and the war doesn't contain the opencv.jar. I added myself the folders WEB-INF/lib in my eclipse project, and I have added inside the opencv.jar but MAVEN or Tomcat didn't considered it apparently... – Alexis Brunet Dec 29 '18 at 15:32
  • Add `opencv.jar` by yourself ? Sound like you are doing something wrong as you use Maven , you should let maven to add it for you by configuring `pom.xml` to include `opencv.jar` for your eclipse project .... – Ken Chan Dec 29 '18 at 15:35
  • I tried again with Maven and this time the opencv.jar is inside the WEB-INF/lib folder, but the error is now different : "java.lang.UnsatisfiedLinkError: no opencv_java342 in java.library.path" – Alexis Brunet Dec 29 '18 at 16:00
  • Please see [How to add a native library in Tomcat](http://stackoverflow.com/a/35552972/1504556). Note that the accepted answer is not the correct solution. – Steve C Jan 02 '19 at 11:45

2 Answers2

1

So I finally sorted it out. Instead of downloading the jar and trying to put it in my maven project, I just added an apparently unofficial maven archetype in my POM :

<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
<dependency>
    <groupId>org.openpnp</groupId>
    <artifactId>opencv</artifactId>
    <version>3.2.0-0</version>
</dependency>

And then I just changed my code from :

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

to

nu.pattern.OpenCV.loadShared();

As suggested here : Java OpenCV from Maven And it worked ! Thanks guys for your help

Alexis Brunet
  • 335
  • 5
  • 15
0

This is a typical problem in eclipse. I have faced this issue several times and have not been able to find any sure shot solution to this.

First Create a WAR of your project and check if you are able to locate opencv.jar in your WAR. DO NOT check it under tomcat's deployment. Create your war using maven install.

If you do not find opencv,jar in your created war, then check your POM again or try adding external jar. But if you do find opencv.jar in your Created WAR, then try below actions.

Following are what worked for me at different occasions:

  1. maven update your project
  2. Clean projects(Make sure you have enabled Build Automatically)
  3. Close the project in work-space and re-open it(I know it sounds silly but worked for me)
  4. Create war of your project, then replace the .class files under the deployed war in your Tomcat Standalone directory with your generated war's .class files. Then restart your tomcat server through eclipse.

For Points 1-3, make sure that you clean up deployments under your tomcat directory

I hope any of the above solutions works for you as well.

Ankur
  • 892
  • 6
  • 11
  • Thanks a lot for your clear answer, but it didn't worked for me. I actually found what was wrong : when using maven, the opencv artifact is apparently not the official one and the syntax i was using to load the library in my code was wrong. I changed System.loadLibrary(Core.NATIVE_LIBRARY_NAME); to nu.pattern.OpenCV.loadShared(); – Alexis Brunet Dec 29 '18 at 17:23