<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: getting runtime error in java in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171038#M45780</link>
    <description>&lt;P&gt;@&lt;A href="https://community.hortonworks.com/users/10115/sahmad43.html"&gt;Sami Ahmad&lt;/A&gt;&lt;/P&gt;&lt;P&gt;By default the current directory (where the *.class is present) is already added to the classpath.  So do not need to worry about it.  The only change you need in your case is to &lt;/P&gt;&lt;P&gt;1. Make sure that the  "HelloOpenCV.class" and "lbpcascade_frontalface.xml" are colocated with following code.&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());&lt;/PRE&gt;&lt;P&gt;OR&lt;/P&gt;&lt;P&gt;2. You should create a JAR where the "HelloOpenCV.class" and "lbpcascade_frontalface.xml" both are present. and then you can use the same code:&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());&lt;/PRE&gt;</description>
    <pubDate>Thu, 10 Nov 2016 14:42:46 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2016-11-10T14:42:46Z</dc:date>
    <item>
      <title>getting runtime error in java</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171035#M45777</link>
      <description>&lt;P&gt; I am following the popular DetectFaceDemo tutorial but I cant seem to run the packaged jar .
[DetectFaceDemo](&lt;A href="http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html"&gt;http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html&lt;/A&gt;) &lt;/P&gt;&lt;P&gt;I have placed the following files in their respect directories as shown below :&lt;/P&gt;&lt;PRE&gt;    [root@hadoop1 java]# pwd
    /root/openCV/opencv/samples/java/sbt/src/main/java
    [root@hadoop1 java]#
    [root@hadoop1 java]# ls
    build.sbt  DetectFaceDemo.java.orig  HelloOpenCV.java  lib  project  target
    [root@hadoop1 java]#
    [root@hadoop1 java]# ls  lib
    libopencv_java249.so  opencv-249.jar
    [root@hadoop1 java]#
    [root@hadoop1 java]# cd ..
    [root@hadoop1 main]# pwd
    /root/openCV/opencv/samples/java/sbt/src/main
    [root@hadoop1 main]# ls
    java  origscala  resources
    [root@hadoop1 main]# ls resources
    AverageMaleFace.jpg  img1.png  img2.png  lbpcascade_frontalface.xml  lena.png
    [root@hadoop1 main]#&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am getting error when I run the jar , I have a feeling the code is not picking up the image and the xml file from the src/main/resources folder ? 
I have though provided the absolute path to the resource folder.&lt;/P&gt;&lt;PRE&gt;    [root@hadoop1 java]# sbt run
    [info] Set current project to DetectFaceDemo (in build file:/root/openCV/opencv/samples/java/sbt/src/main/java/)
    [info] Running HelloOpenCV
    [info] Hello, OpenCV
    [info]
    [info] Running DetectFaceDemo
    [error] Exception in thread "main" java.lang.NullPointerException
    [error]         at DetectFaceDemo.run(HelloOpenCV.java:20)
    [error]         at HelloOpenCV.main(HelloOpenCV.java:48)
    java.lang.RuntimeException: Nonzero exit code returned from runner: 1
            at scala.sys.package$.error(package.scala:27)
    [trace] Stack trace suppressed: run last compile:run for the full output.
    [error] (compile:run) Nonzero exit code returned from runner: 1
    [error] Total time: 1 s, completed Nov 9, 2016 11:20:34 PM&lt;/PRE&gt;&lt;P&gt;The source code is below :&lt;/P&gt;&lt;PRE&gt;[root@hadoop1 java]# more HelloOpenCV.java
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfRect;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.objdetect.CascadeClassifier;
    
    //
    // Detects faces in an image, draws boxes around them, and writes the results
    // to "faceDetection.png".
    //
    class DetectFaceDemo {
      public void run() {
        System.out.println("\nRunning DetectFaceDemo");
    
        // Create a face detector from the cascade file in the resources
        // directory.
        CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml").getPath());
        Mat image = Highgui.imread(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lena.png").getPath());
    
        // Detect faces in the image.
        // MatOfRect is a special container class for Rect.
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);
    
        System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    
        // Draw a bounding box around each face.
        for (Rect rect : faceDetections.toArray()) {
            Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255
    , 0));
        }
    
        // Save the visualized detection.
        String filename = "faceDetection.png";
        System.out.println(String.format("Writing %s", filename));
        Highgui.imwrite(filename, image);
      }
    }
    
    public class HelloOpenCV {
      public static void main(String[] args) {
        System.out.println("Hello, OpenCV");
    
        // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new DetectFaceDemo().run();
      }
    }
    [root@hadoop1 java]#


&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 12:50:20 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171035#M45777</guid>
      <dc:creator>aliyesami</dc:creator>
      <dc:date>2016-11-10T12:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: getting runtime error in java</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171036#M45778</link>
      <description>&lt;P&gt;@&lt;A href="https://community.hortonworks.com/users/10115/sahmad43.html"&gt;Sami Ahmad&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The NullPointerException seems to be coming from the following line 20 which is the following in your code:&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml").getPath());
&lt;/PRE&gt;&lt;P&gt;I am suspecting that you might not have placed the file in that location with proper read/write permission. &lt;/P&gt;&lt;P&gt;As the code is using  "getClass().getResource(...)" method approach here so it means the class should be present relative to your  "HelloOpenCV.class" directory.    So please place the XML file  "lbpcascade_frontalface.xml"   in the same directory where you have the "HelloOpenCV.class" and then  change the code to give the relative path:&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 13:09:04 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171036#M45778</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-11-10T13:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: getting runtime error in java</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171037#M45779</link>
      <description>&lt;P&gt;found the solution online &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;put the following in the code to find the  classpath and copy the "xml" and "png" files to this location .
 
 System.out.println(System.getProperty("java.class.path"));&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 14:24:12 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171037#M45779</guid>
      <dc:creator>aliyesami</dc:creator>
      <dc:date>2016-11-10T14:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: getting runtime error in java</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171038#M45780</link>
      <description>&lt;P&gt;@&lt;A href="https://community.hortonworks.com/users/10115/sahmad43.html"&gt;Sami Ahmad&lt;/A&gt;&lt;/P&gt;&lt;P&gt;By default the current directory (where the *.class is present) is already added to the classpath.  So do not need to worry about it.  The only change you need in your case is to &lt;/P&gt;&lt;P&gt;1. Make sure that the  "HelloOpenCV.class" and "lbpcascade_frontalface.xml" are colocated with following code.&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());&lt;/PRE&gt;&lt;P&gt;OR&lt;/P&gt;&lt;P&gt;2. You should create a JAR where the "HelloOpenCV.class" and "lbpcascade_frontalface.xml" both are present. and then you can use the same code:&lt;/P&gt;&lt;PRE&gt;CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 14:42:46 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/getting-runtime-error-in-java/m-p/171038#M45780</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-11-10T14:42:46Z</dc:date>
    </item>
  </channel>
</rss>

