Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / introduction / java_eclipse / java_eclipse.markdown
1 Using OpenCV Java with Eclipse {#tutorial_java_eclipse}
2 ==============================
3
4 @prev_tutorial{tutorial_java_dev_intro}
5 @next_tutorial{tutorial_clojure_dev_intro}
6
7
8 Since version 2.4.4 [OpenCV supports Java](http://opencv.org/opencv-java-api.html). In this tutorial
9 I will explain how to setup development environment for using OpenCV Java with Eclipse in
10 **Windows**, so you can enjoy the benefits of garbage collected, very refactorable (rename variable,
11 extract method and whatnot) modern language that enables you to write code with less effort and make
12 less mistakes. Here we go.
13
14 Configuring Eclipse
15 -------------------
16
17 First, obtain a fresh release of OpenCV [from download page](http://opencv.org/releases.html) and
18 extract it under a simple location like `C:\OpenCV-2.4.6\`. I am using version 2.4.6, but the steps
19 are more or less the same for other versions.
20
21 Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any
22 project. Launch Eclipse and select Window --\> Preferences from the menu.
23
24 ![](images/1-window-preferences.png)
25
26 Navigate under Java --\> Build Path --\> User Libraries and click New....
27
28 ![](images/2-user-library-new.png)
29
30 Enter a name, e.g. OpenCV-2.4.6, for your new library.
31
32 ![](images/3-library-name.png)
33
34 Now select your new user library and click Add External JARs....
35
36 ![](images/4-add-external-jars.png)
37
38 Browse through `C:\OpenCV-2.4.6\build\java\` and select opencv-246.jar. After adding the jar,
39 extend the opencv-246.jar and select Native library location and press Edit....
40
41 ![](images/5-native-library.png)
42
43 Select External Folder... and browse to select the folder `C:\OpenCV-2.4.6\build\java\x64`. If you
44 have a 32-bit system you need to select the x86 folder instead of x64.
45
46 ![](images/6-external-folder.png)
47
48 Your user library configuration should look like this:
49
50 ![](images/7-user-library-final.png)
51
52 Testing the configuration on a new Java project
53 -----------------------------------------------
54
55 Now start creating a new Java project.
56
57 ![](images/7_5-new-java-project.png)
58
59 On the Java Settings step, under Libraries tab, select Add Library... and select OpenCV-2.4.6, then
60 click Finish.
61
62 ![](images/8-add-library.png)
63
64 ![](images/9-select-user-lib.png)
65
66 Libraries should look like this:
67
68 ![](images/10-new-project-created.png)
69
70 Now you have created and configured a new Java project it is time to test it. Create a new java
71 file. Here is a starter code for your convenience:
72 @code{.java}
73 import org.opencv.core.Core;
74 import org.opencv.core.CvType;
75 import org.opencv.core.Mat;
76
77 public class Hello
78 {
79    public static void main( String[] args )
80    {
81       System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
82       Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
83       System.out.println( "mat = " + mat.dump() );
84    }
85 }
86 @endcode
87 When you run the code you should see 3x3 identity matrix as output.
88
89 ![](images/11-the-code.png)
90
91 That is it, whenever you start a new project just add the OpenCV user library that you have defined
92 to your project and you are good to go. Enjoy your powerful, less painful development environment :)
93
94 Running Java code with OpenCV and MKL dependency
95 ------------------------------------------------
96
97 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
98 that use Intel MKL:
99 > Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
100
101 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).
102 Your command line should be something similar to this (add `$LD_PRELOAD:` before if you have already set the `LD_PRELOAD` variable):
103 > export LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_core.so:/opt/intel/mkl/lib/intel64/libmkl_sequential.so
104
105 Then, run the Eclipse IDE from a terminal that have this environment variable set (`echo $LD_PRELOAD`) and the error should disappear.