Home » Java » java.lang.ClassNotFoundException – Eclipse with OpenCV-Exceptionshub

java.lang.ClassNotFoundException – Eclipse with OpenCV-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

When I run my below code I get the below error, but it worked before and I am not sure what Eclipse got and is not good anymore

Error: Unable to initialize main class src.convolution
Caused by: java.lang.NoClassDefFoundError: org/opencv/core/Mat

package src;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class convolution {
   public static void main( String[] args ) {

      try {
         int kernelSize = 9;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

         Mat source = Imgcodecs.imread("C:/Users/B & B/Desktop/ProcIMG/grayscale.jpg", Imgcodecs.IMREAD_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
            {
               put(0,0,-3);
               put(0,1,-3);
               put(0,2,-3);

               put(1,0-3);
               put(1,1,0);
               put(1,2,-3);

               put(2,0,5);
               put(2,1,5);
               put(2,2,5);
            }
         };       

         Imgproc.filter2D(source, destination, -1, kernel);
         Imgcodecs.imwrite("C:/Users/B & B/Desktop/ProcIMG/output.jpg", destination);

      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}
How to&Answers:

It could be .classpath issue. Try following steps

  • Delete Project from Eclipse don’t delete content from disk.
  • remove .classpath and .project files
  • re-import project in Eclipse