Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

can't run a jar file

avatar
Super Collaborator

I can successfully compile the program below but when I run it I get errors :

[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("/lbpcascade_frontalface.xml").getPath());
    Mat image = Highgui.imread(getClass().getResource("/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();
  }
}

the build file is shown below. The "-Djava.library.path " to the local "./lib" folder where the opencv-249.jar file is located

[root@hadoop1 java]# more build.sbt
name := "DetectFaceDemo"
scalaVersion := "2.11.6"
scalacOptions ++= Seq(
  "-unchecked",
  "-deprecation",
  "-optimize",
  "-Xlint"
)
javaOptions in run += "-Djava.library.path=./lib"
lazy val root = (project in file("."))
fork := true


[root@hadoop1 java]# ls ./lib
opencv-249.jar
[root@hadoop1 java]#

if the "sbt package" successfully creates a jar file but when I run it using SBT I get the following error :

he is complaining about a file "opencv_java249" where as the file that's produced by compilation is "opencv-249.jar"

[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]# sbt run
[info] Set current project to DetectFaceDemo (in build file:/root/openCV/opencv/samples/java/sbt/src/main/java/)
[info] Compiling 1 Java source to /root/openCV/opencv/samples/java/sbt/src/main/java/target/scala-2.11/classes...
[info] Running HelloOpenCV
[info] Hello, OpenCV
[error] Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path
[error]         at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1889)
[error]         at java.lang.Runtime.loadLibrary0(Runtime.java:849)
[error]         at java.lang.System.loadLibrary(System.java:1088)
[error]         at HelloOpenCV.main(HelloOpenCV.java:47)
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 9:04:45 PM

on the other hand if I try to run it via java I get different error as follows :

[root@hadoop1 scala-2.11]# java -cp detectfacedemo_2.11-0.1-SNAPSHOT.jar HelloOpenCV
Hello, OpenCV
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
        at HelloOpenCV.main(HelloOpenCV.java:47)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 1 more
1 ACCEPTED SOLUTION

avatar
Super Collaborator

the error was fixed by moving the following files in the respective directories .

/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]#

View solution in original post

2 REPLIES 2

avatar
Super Collaborator

I am following this tutorial and also copied the library file they mention but still same errors

http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html

/root/openCV/opencv/samples/java/sbt/src/main/resources
[root@hadoop1 resources]# ls
AverageMaleFace.jpg  img1.png  img2.png  lbpcascade_frontalface.xml
[root@hadoop1 resources]#


avatar
Super Collaborator

the error was fixed by moving the following files in the respective directories .

/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]#