Skip to content
Advertisement

How to build OpenCV with Java under Linux using command line?(Gonna use it in MapReduce)

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

  1. Installed vim
  2. Installed g++
  3. Installed cmake
  4. installed cmake-curses-gui
  5. installed java7 JVM
  6. 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.

Advertisement

Answer

Probably what you’re missing is ant, which is a java oriented build tool.

When you go:

cmake -D BUILD_SHARED_LIBS=OFF ../opencv-2.4.5/

(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):

--   Java:
--     ant:                         /usr/bin/ant (ver 1.8.2)
--     JNI:                         /usr/lib/jvm/java-6-openjdk/include /usr/lib/jvm/java-6-openjdk/include /usr/lib/jvm/java-6-openjdk/include
--     Java tests:                  YES

When I first ran cmake, I had this:

--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java tests:                  YES

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:

sudo apt-get install ant

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 then ls -l to follow a series of symbolic links, to identify where my java 1.7 install was. You could also use locate jni.h and work up the tree, or simply look around /usr/lib/jvm or similar.

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

You probably want to check that you’ve got that right with a couple of

ls $JAVA_HOME/bin/javac
/usr/lib/jvm/java-7-openjdk-amd64/bin/javac

ls $JAVA_HOME/include/jni.h
/usr/lib/jvm/java-7-openjdk-amd64/include/jni.h

Interestingly when I ran the cmake command again, it picked up a different JNI folder:

--     JNI:                         /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include

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 called opencv-245.jar as that’s the version of the code I downloaded).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement