16

Where are the Java jar libraries for the openCV core extensions so that I can import it in my java code?

I cannot find a single place where they have taught how to get everything set up properly. I am using Ubuntu 12.04 and I have openCV installed. I want to use it in eclipse IDE, and eclipse needs a jar file so that I can use openCV functions. I saw the following link which has used the import org.opencv.core.Core;

How can I get those .jar files?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
theAndDev
  • 662
  • 2
  • 11
  • 33
  • 1
    that is how: http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html#java-eclipse – Abdul Wasae Oct 30 '15 at 11:36
  • 1
    @AbdulWasae That does not describe how to achieve the jar file. It is what to do when you have the jar file. I have also come across this recently when the need for building the opencv source code at the command line. This most often comes up when adding in contrib modules that are not part of the main source tree. The jar does not exist after a successful build. Just the supporting .so files. – Jay Snayder Aug 24 '16 at 12:05

1 Answers1

20

You can find jars for openCV for linux kicking around the internet like at this link. However it won't work unless you have the native libraries openCV needs to do its work.

A sure way to get openCV available in your eclipse java project is to compile your own jar file from source to make it available as described here: https://udallascs.wordpress.com/2014/03/30/adding-opencv-and-configuring-to-work-with-eclipse-and-java/

Here is the jist of instructions for Linux, open a terminal and run these commands:

cd ~
mkdir Vision
cd Vision
git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=OFF ..
make -j8

If everything succeeded, then your jars will be under the be under the bin directory:

./bin/opencv-300.jar

Move that opencv-300.jar into the lib directory in your project and include it as an external jar. Here is the barebones program that uses it.

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

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        System.out.println(System.getProperty("java.library.path"));
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("m = " + m.dump());
    }
}

Within eclipse, your jar file will need the native libraries you built earlier to be available. So in eclipse, navigate to:

Project->Properties->Java Build Path->Libraries tab-> Add external jars -> opencv-300.jar

Then double click: "Native library location" and enter in the build/lib where you built it, in my case: /home/el/Vision/opencv/build/lib

Run the java program, the program prints:

Welcome to OpenCV 3.0.0-dev
/home/el/Vision/opencv/build/lib
m = [1,   0,   0;
     0,   1,   0;
     0,   0,   1]

If you want to give this program to someone else and have them be able to run it, they will need to have openCV version 3.0.0 also available on their system, or else the java program will not find the libraries, and immediately exit.

Why is this so hard, why is this not just a simple jar?

Because openCV is written in C, and the jar file is just a window into that C world. So we have to make a Rube Goldberg machine to make the methods in OpenCV available to your java application.

yilmazburk
  • 907
  • 9
  • 17
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • 4
    thanks for the help but then i did not find any jar files even after making and installing the openCV provided from the above link. I am stuck here can you please specify the actions i need to take. – theAndDev Sep 19 '13 at 05:28
  • 9
    Things would be so much simplified if there were a maven repository... – Bachi Sep 09 '15 at 09:15
  • Any idea why the resulting jar file does not contain the android bindings, e.g. CameraBridgeViewBase.java ? – Clayton Louden Jun 24 '16 at 21:20
  • Android things are in a different download https://sourceforge.net/projects/opencvlibrary/files/opencv-android/ – Fabio Nov 05 '16 at 03:59
  • 2
    There is no "expand the opencv-300.jar". – Schütze Nov 11 '16 at 10:19
  • 7
    So it's basically pointless then to make an executable program from OpenCV given the invariably non-technical people who download my program will have to also go through the complicated steps required to get it installed on their system? – Martin Erlic Aug 10 '17 at 15:01
  • 1
    If you jar is not generated is because you need Apache Ant installed. Pay attention to the output of cmake -D BUILD_SHARED_LIBS=OFF .. You need something like this: -- Java: -- ant: /usr/bin/ant (ver 1.9.6) -- JNI: /usr/lib/jvm/java-8-oracle/include /usr/lib/jvm/java-8-oracle/include/linux /usr/lib/jvm/java-8-oracle/include – maxtorzito May 25 '18 at 18:05