Recently I'm trying OpenCV out for my graduation project. I've had some success under Windows enviroment. And because with Windows package of OpenCV it comes with pre-built libraries, so I don't have to worry about how to build them. But since the project is suppose to run on a cluster with CentOS as host OS for each node, I have to know how to correctly compile, and run these library under Linux enviroment.
I've set up a VM with VirtualBox and installed Ubuntu 13.04 on it. But so far I still can't find a way to adjust the settings of CMakeList to make Java a build target. (A jar, and a native library so I can import and use them in MapReduce program) Following is the official tutorial of desktop Java for OpenCV
http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html
The part about the compiling is pretty brief. So I still can't quite understand it. Is it because I missed some dependency for Java? I've already got JVM(Jre-7u7) installed. Or is it because I didn't configure CMakeList.txt correctly?
Here is the things I've done so far, other than that it's a complete clean Ubuntu
- Installed vim
- Installed g++
- Installed cmake
- installed cmake-curses-gui
- installed java7 JVM
- download OpenCV package for Linux
The target enviroment is Cluster with CentOS as host OS for each node, and the project is a MapReduce program.
Thanks in advance.
For someone directed here from google :
If your JRE crashes after you run the java tutorials then most probably you have the python Bindings installed as well (cv2.so) . You will have to re-make OpenCV without those bindings
cmake -DBUILD_SHARED_LIBS=OFF -D BUILD_NEW_PYTHON_SUPPORT=NO
That solved the problem for me.
Probably what you're missing is
ant
, which is a java oriented build tool.When you go:
(i.e. where you're configuring the make for your machine), check for the section that says java. It should say something like this (possibly with different paths/versions):
When I first ran cmake, I had this:
Which indicated it couldn't find
ant
, and so it didn't create a .jar file. I simply use the version that's in the repository:I ran cmake again, with the above options, which got the path to ant (and I got JNI for free!).
Note: You probably want to read the output of cmake fairly carefully as it tells you what it's going to build on your machine, and you may have some missing libraries.
If you find JNI is still missing.
cmake is (mostly? first? conveniently? can be interpreted as?) looking for jni.h, which should be specifically
$JAVA_HOME/include/jni.h
So you need to set $JAVA_HOME to the home folder of your jdk.I used
which javac
and thenls -l
to follow a series of symbolic links, to identify where my java 1.7 install was. You could also uselocate jni.h
and work up the tree, or simply look around/usr/lib/jvm
or similar.You probably want to check that you've got that right with a couple of
Interestingly when I ran the cmake command again, it picked up a different JNI folder:
Once you've got that, run the
make
command. If you watch obsessively, you'll see a bunch of .java files trundle past. Either way you'll end up with a .jar file in the ./bin folder. (in my case it's calledopencv-245.jar
as that's the version of the code I downloaded).I tried to build OpenCV 3 on Ubuntu 14 and encountered similar problems with Oracle java 8 JDK.
These are the steps I preformed to build and run a java program using the java OpenCV bindings.
FindJNI. Since the "FindJNI" module of cmake 3 doesn't support the detection of Oracle Java 8, we have to add our Java 8 directories manually. Therefore, locate the file
FindJNI.cmake
in your cmake directory,e.g/home/foo/bar/cmake-3.2.2-Linux-x86_64/share/cmake-3.2/Modules/FindJNI.cmake
Goto the
JAVA_APPEND_LIBRARY_DIRECTORIES
variable and add the path to your java lib architecture directory, e.g./home/foo/bar/jdk1.8.0_45/lib/amd64
Goto the
JAVA_AWT_INCLUDE_DIRECTORIES
variable and add the path to your java include directory, e.g./home/foo/bar/jdk1.8.0_45/include
Build. The shared lib flag tiggers the build of the java bindings, if your modified FindJNI found your Oracle 8 JDK.
Write Testcode. In your
build
directory you should see a jar file like./bin/opencv-300.jar
. This file can be added to your java build dependencies, e.g. in your favorite IDE.Run Testcode. You have to add the opencv lib path to your VM options:
[1] Note: Depending on your personal needs or taste you can install various other dependencies before building, e.g. compare http://rodrigoberriel.com/2014/10/installing-opencv-3-0-0-on-ubuntu-14-04/
Since version 3.1.0 of OpenCV there is a Maven POM project under
OpenCV_Root_Directory/platforms/maven
. With Maven installed on your Linux machine (using package manager or manually installed) this project will ensure you have the correct dependencies installed, build the native and Java libraries. See the README in the above directory.