1 Using OpenCV Java with Eclipse {#tutorial_java_eclipse}
2 ==============================
4 Since version 2.4.4 [OpenCV supports Java](http://opencv.org/opencv-java-api.html). In this tutorial
5 I will explain how to setup development environment for using OpenCV Java with Eclipse in
6 **Windows**, so you can enjoy the benefits of garbage collected, very refactorable (rename variable,
7 extract method and whatnot) modern language that enables you to write code with less effort and make
8 less mistakes. Here we go.
13 First, obtain a fresh release of OpenCV [from download page](http://opencv.org/releases.html) and
14 extract it under a simple location like `C:\OpenCV-2.4.6\`. I am using version 2.4.6, but the steps
15 are more or less the same for other versions.
17 Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any
18 project. Launch Eclipse and select Window --\> Preferences from the menu.
20 ![](images/1-window-preferences.png)
22 Navigate under Java --\> Build Path --\> User Libraries and click New....
24 ![](images/2-user-library-new.png)
26 Enter a name, e.g. OpenCV-2.4.6, for your new library.
28 ![](images/3-library-name.png)
30 Now select your new user library and click Add External JARs....
32 ![](images/4-add-external-jars.png)
34 Browse through `C:\OpenCV-2.4.6\build\java\` and select opencv-246.jar. After adding the jar,
35 extend the opencv-246.jar and select Native library location and press Edit....
37 ![](images/5-native-library.png)
39 Select External Folder... and browse to select the folder `C:\OpenCV-2.4.6\build\java\x64`. If you
40 have a 32-bit system you need to select the x86 folder instead of x64.
42 ![](images/6-external-folder.png)
44 Your user library configuration should look like this:
46 ![](images/7-user-library-final.png)
48 Testing the configuration on a new Java project
49 -----------------------------------------------
51 Now start creating a new Java project.
53 ![](images/7_5-new-java-project.png)
55 On the Java Settings step, under Libraries tab, select Add Library... and select OpenCV-2.4.6, then
58 ![](images/8-add-library.png)
60 ![](images/9-select-user-lib.png)
62 Libraries should look like this:
64 ![](images/10-new-project-created.png)
66 Now you have created and configured a new Java project it is time to test it. Create a new java
67 file. Here is a starter code for your convenience:
69 import org.opencv.core.Core;
70 import org.opencv.core.CvType;
71 import org.opencv.core.Mat;
75 public static void main( String[] args )
77 System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
78 Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
79 System.out.println( "mat = " + mat.dump() );
83 When you run the code you should see 3x3 identity matrix as output.
85 ![](images/11-the-code.png)
87 That is it, whenever you start a new project just add the OpenCV user library that you have defined
88 to your project and you are good to go. Enjoy your powerful, less painful development environment :)
90 Running Java code with OpenCV and MKL dependency
91 ------------------------------------------------
93 You may get the following error (e.g. on Ubuntu) if you have built OpenCV with MKL library with some Java code that calls OpenCV functions
95 > Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
97 One solution to solve this on Linux consists in preloading the Intel MKL library (either run the command in a terminal or add it to your `.bashrc` file).
98 Your command line should be something similar to this (add `$LD_PRELOAD:` before if you have already set the `LD_PRELOAD` variable):
99 > export LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_sequential.so
101 Then, run the Eclipse IDE from a terminal that have this environment variable set (`echo $LD_PRELOAD`) and the error should disappear.