1

I'm trying to compile and run a simple example of Java code using OpenCV library, on Ubuntu 14.04 64bits, found in OpenCV's documentation: Java OpenCV Documentation

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;

class SimpleSample
{
    static
    {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args)
    {
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
        System.out.println("OpenCV Mat: " + m);
        Mat mr1 = m.row(1);
        mr1.setTo(new Scalar(1));
        Mat mc5 = m.col(5);
        mc5.setTo(new Scalar(5));
        System.out.println("OpenCV Mat data:\n" + m.dump());
    }
}

Since it is just a simple test, I really don't want to use any sophisticated building system. So I'm compiling with the following command:

javac -cp "/usr/share/OpenCV/java/opencv-248.jar" SimpleSample.java

However I'm unable to run the application, because when I call: java SimpleSample

I face the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core

at SimpleSample.<clinit>(SimpleSample.java:10)

Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

Can someone help me running this application?

0

3 Answers 3

2

The opencv-248.jar is not on the CLASSPATH. You can run it as follows:

java -cp /usr/share/OpenCV/java/opencv-248.jar:. SimpleSample

Compile from the source and copy libopencv_java248.so into $JAVA_HOME/jre/lib/amb64/ directory. This platform specific file is not included in the jar file. Running the sample program without this file installed would get "java.lang.UnsatisfiedLinkError: no opencv_java248 in java.library.path" error. Also note that Apache Ant need to be installed for this platform specific file to be generated during compilation.

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

2 Comments

This doesn't work. java -cp /usr/share/OpenCV/java/opencv-248.jar:. SimpleSample Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java248 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at SimpleSample.<clinit>(SimpleSample.java:10)
You need to install libopencv_java248.so in your $JAVA_HOME/jre/lib/amb64/ directory for the opencv-248.jar to work. To get libopencv_java248.so, compile it from the source.
1

I managed to solve the problem. In Ubuntu 14.04 64bit you can run the code from command line with the following command:

LD_LIBRARY_PATH=/usr/lib/jni/ java -cp /usr/share/OpenCV/java/opencv-248.jar:. SimpleSample

Comments

0

The OpenCV native binary library path (in my case libopencv_java343.dylib) can be defined with -D:

java -cp /opt/local/share/OpenCV/java/opencv-343.jar:. -Djava.library.path=/opt/local/share/OpenCV/java/ SimpleCV

Comments

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.