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

5
  • Check if the WAR after package contain web-inf/lib/opencv.jar ? Commented Dec 29, 2018 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... Commented Dec 29, 2018 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 .... Commented Dec 29, 2018 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" Commented Dec 29, 2018 at 16:00
  • Please see How to add a native library in Tomcat. Note that the accepted answer is not the correct solution. Commented Jan 2, 2019 at 11:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.