ChitraPathak / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

cannot run javacv example, ClassNotFoundException (Mac OS X) #127

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Install opencv from Mac Ports ( 
http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port )
2. Downloaded javacv
3. Created example Smoother.java:

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

public class Smoother {
    public static void main(String filename) { 
        IplImage image = cvLoadImage(filename);
        if (image != null) {
            cvSmooth(image, image, CV_GAUSSIAN, 3);
            cvSaveImage(filename, image);
            cvReleaseImage(image);
        }
    }
}

4. compiled
$ javac -cp ./javacv.jar Smoother.java

5. ran
$ java -cp ./javacv.jar Smoother

What is the expected output? What do you see instead?

$ java Smoother
Exception in thread "main" java.lang.NoClassDefFoundError: 
com/googlecode/javacv/cpp/opencv_core$CvArr
Caused by: java.lang.ClassNotFoundException: 
com.googlecode.javacv.cpp.opencv_core$CvArr
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
$ java -cp ./javacv.jar Smoother
Exception in thread "main" java.lang.NoClassDefFoundError: Smoother
Caused by: java.lang.ClassNotFoundException: Smoother
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
$ java -cp ./javacv.jar:./ Smoother
Exception in thread "main" java.lang.NoSuchMethodError: main

What version of the product are you using? On what operating system?
 * Mac OS X 10.7.2
 * OpenCV 2.3.1a (from MacPorts)
 * JavaCV (bin 2011-10-01)

Please provide any additional information below.

Not a dup of 
http://code.google.com/p/javacv/issues/detail?id=125&can=1&sort=-id, this is a 
test from compiling at the command line (removing any potential Eclipse 
complications)

Original issue reported on code.google.com by andrew.c...@gmail.com on 3 Nov 2011 at 5:21

GoogleCodeExporter commented 9 years ago
Hum, looks like MacPorts does not add its libraries in the path by default. We 
can work around this by setting the DYLD_FALLBACK_LIBRARY_PATH environment 
variable, e.g.:
    export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib/
This directory will be hardcoded next time I recompile JavaCV with MacPorts, 
well unless OpenCV 2.3.2 or something comes out and MacPorts takes another few 
months to follow...

Original comment by samuel.a...@gmail.com on 4 Nov 2011 at 4:12

GoogleCodeExporter commented 9 years ago
Well, oddly enough now I can run the javacv example just fine in Eclipse, 
though still having some issue with the classpath on the command line.

What changed is I rebuilt libpng in Mac Ports.  I can link to the thread 
discussing it there if you wish.

Original comment by andrew.c...@gmail.com on 4 Nov 2011 at 8:49

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I got it. Thanks anyway.

Original comment by bphan...@gmail.com on 22 Nov 2011 at 3:31

GoogleCodeExporter commented 9 years ago
Mind sharing your solution?

Original comment by andrew.c...@gmail.com on 22 Nov 2011 at 2:09

GoogleCodeExporter commented 9 years ago
Typing the export command above in a Terminal window does not work?

Original comment by samuel.a...@gmail.com on 26 Nov 2011 at 6:04

GoogleCodeExporter commented 9 years ago
If you are running it in Eclipse simply add the environment variable 
DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib  in the debug/run configuration and 
that should work.  Otherwise if you run from the command line having it set in 
your env should also work.

Original comment by triggull...@gmail.com on 29 Nov 2011 at 5:37

GoogleCodeExporter commented 9 years ago
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib/

also worked for me.
(OSX)

Original comment by jerda...@speakeasy.net on 2 Dec 2011 at 6:35

GoogleCodeExporter commented 9 years ago
Fixed in latest release!

Original comment by samuel.a...@gmail.com on 8 Jan 2012 at 3:46

GoogleCodeExporter commented 9 years ago
hello we met the same problem,
My environment is : java 7, open cv 2.4.8, javacv 0.7

The problem below:

SEVERE: Servlet.service() for servlet [UploadServlet] in context with path 
[/ImgWeb] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: com.googlecode.javacv.cpp.opencv_core$CvArr
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1666)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
    at com.matrix.servlet.UploadServlet.doPost(UploadServlet.java:43)
    at com.matrix.servlet.UploadServlet.doGet(UploadServlet.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I write a function:
public static double getSimilarity(String src, String template){
        double similarity = 0.0d;
        IplImage img, tpl, res;
        CvPoint minloc = new CvPoint();
        CvPoint maxloc = new CvPoint();
        double minval[] = new double[5];
        double maxval[] = new double[5];

        int img_width, img_height;
        int tpl_width, tpl_height;
        int res_width, res_height;

        img = cvLoadImage(src, CV_LOAD_IMAGE_COLOR);//CV_LOAD_IMAGE_COLOR
        tpl = cvLoadImage(template, CV_LOAD_IMAGE_COLOR);//CV_LOAD_IMAGE_COLOR

        img_width = img.width();
        img_height = img.height();
        tpl_width = tpl.width();
        tpl_height = tpl.height();

        if (img_height < tpl_height || img_width < tpl_width) {

            System.out.println("inter");
            IplImage tempImg = img.clone();
            img = tpl.clone();
            tpl = tempImg.clone();
            cvReleaseImage(tempImg);

            res_width = tpl_width-img_width +1;
            res_height = tpl_height - img_height +1;

            System.out.println("res_width: " + res_width + "res_height: " + res_height);

        } else {
            res_width = img_width - tpl_width + 1;
            res_height = img_height - tpl_height + 1;

        }

        System.out.println("res_width: " + res_width + "res_height: " + res_height);

        res = cvCreateImage(cvSize(res_width, res_height), IPL_DEPTH_32F, 1);
        cvMatchTemplate(img, tpl, res, CV_TM_CCORR_NORMED );
        cvMinMaxLoc(res, minval, maxval, minloc, maxloc, null);

        System.out.println("min: " + minval[0]);

        CvPoint pt1;
        CvPoint pt2;
        CvRect rect;
        rect = cvRect(maxloc.x(), maxloc.y(), tpl.width(), tpl.height());// 最佳的匹配区域
        pt1 = cvPoint(rect.x(), rect.y());
        pt2 = cvPoint(rect.x() + rect.width(), rect.y() + rect.height());
        cvRectangle(img, pt1, pt2, cvScalar(0, 0, 0, 0), 1, 8, 0);

        cvNamedWindow("reference", CV_WINDOW_AUTOSIZE);
        cvShowImage("reference", img);

        cvWaitKey(0);

        similarity = maxval[0];

        /* free memory*/
        cvReleaseImage(img);
        cvReleaseImage(tpl);
        cvReleaseImage(res);

        return similarity;
    }
when I run it in the main function there is no problem, but when I invok the 
function 
in at servlet or rest service there cause a class not found exception, How can 
I solve the problem

Original comment by josely...@gmail.com on 18 Apr 2014 at 6:44

GoogleCodeExporter commented 9 years ago
hello we met the same problem,
My environment is : java 7, open cv 2.4.8, javacv 0.7

The problem below:

SEVERE: Servlet.service() for servlet [UploadServlet] in context with path 
[/ImgWeb] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: com.googlecode.javacv.cpp.opencv_core$CvArr
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1666)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
    at com.matrix.servlet.UploadServlet.doPost(UploadServlet.java:43)
    at com.matrix.servlet.UploadServlet.doGet(UploadServlet.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I write a function:
public static double getSimilarity(String src, String template){
        double similarity = 0.0d;
        IplImage img, tpl, res;
        CvPoint minloc = new CvPoint();
        CvPoint maxloc = new CvPoint();
        double minval[] = new double[5];
        double maxval[] = new double[5];

        int img_width, img_height;
        int tpl_width, tpl_height;
        int res_width, res_height;

        img = cvLoadImage(src, CV_LOAD_IMAGE_COLOR);//CV_LOAD_IMAGE_COLOR
        tpl = cvLoadImage(template, CV_LOAD_IMAGE_COLOR);//CV_LOAD_IMAGE_COLOR

        img_width = img.width();
        img_height = img.height();
        tpl_width = tpl.width();
        tpl_height = tpl.height();

        if (img_height < tpl_height || img_width < tpl_width) {

            System.out.println("inter");
            IplImage tempImg = img.clone();
            img = tpl.clone();
            tpl = tempImg.clone();
            cvReleaseImage(tempImg);

            res_width = tpl_width-img_width +1;
            res_height = tpl_height - img_height +1;

            System.out.println("res_width: " + res_width + "res_height: " + res_height);

        } else {
            res_width = img_width - tpl_width + 1;
            res_height = img_height - tpl_height + 1;

        }

        System.out.println("res_width: " + res_width + "res_height: " + res_height);

        res = cvCreateImage(cvSize(res_width, res_height), IPL_DEPTH_32F, 1);
        cvMatchTemplate(img, tpl, res, CV_TM_CCORR_NORMED );
        cvMinMaxLoc(res, minval, maxval, minloc, maxloc, null);

        System.out.println("min: " + minval[0]);

        CvPoint pt1;
        CvPoint pt2;
        CvRect rect;
        rect = cvRect(maxloc.x(), maxloc.y(), tpl.width(), tpl.height());// 最佳的匹配区域
        pt1 = cvPoint(rect.x(), rect.y());
        pt2 = cvPoint(rect.x() + rect.width(), rect.y() + rect.height());
        cvRectangle(img, pt1, pt2, cvScalar(0, 0, 0, 0), 1, 8, 0);

        cvNamedWindow("reference", CV_WINDOW_AUTOSIZE);
        cvShowImage("reference", img);

        cvWaitKey(0);

        similarity = maxval[0];

        /* free memory*/
        cvReleaseImage(img);
        cvReleaseImage(tpl);
        cvReleaseImage(res);

        return similarity;
    }
when I run it in the main function there is no problem, but when I invok the 
function 
in at servlet or rest service there cause a class not found exception, How can 
I solve the problem.

Original comment by josely...@gmail.com on 18 Apr 2014 at 6:44

GoogleCodeExporter commented 9 years ago
hey  josely...@gmail.com

I am having the same situation with struts2, with main it runs fine but when 
itegrating with struts2 it gives the exception....hoping someone will give us 
the solution..quickly.

Original comment by abhijitb...@gmail.com on 23 Apr 2014 at 10:33

GoogleCodeExporter commented 9 years ago
We usually need to do something special for JNI and containers like Struts. 
Please read their documentation and follow what they say we should do in the 
case of JNI.

Original comment by samuel.a...@gmail.com on 7 May 2014 at 1:40

GoogleCodeExporter commented 9 years ago
Hey , are there a solution for this issue?

Original comment by topcum...@gmail.com on 12 Apr 2015 at 3:25