JavaCV: Setup and Troubleshooting

Introduction

I decided to use JavaCV, for a project that needs to do some (hopefully light) image recognition. OpenCV is a widely used library written in C/C++ to do real-time “computer-vision” and JavaCV is a nice wrapper around it to give Java developers access to OpenCV’s power. So far the set up has been a nightmare, and I would like to go over what I did to get it working on my machine including some of the problems I ran in to.

I more or less followed the instructions here, but some of it I didnt have to do and other parts were slightly out of date.

At a high level, you will only need to do 3 things to use JavaCV in your java project:

  1. Download/Install OpenCV
  2. Download/Install JavaCV
  3. Add JavaCV to your project’s build path

Sounds easy, right? Well the installation of these libraries requires some manual configuration steps which, if not done properly, can lead to some headaches.

My Environment

OS: Windows 7 SP1 (64-Bit)
IDE: Spring Tool Suite v3.6.1.RELEASE (based on Eclipse Kepler SR2 (4.3.2))
Java: jdk1.8.0_11

Installation – tl;dr version

  1. Download OpenCV-2.3.1-win-superpack.exe
  2. Install to C:\
  3. Add C:\opencv\build\x64\vc10\bin;C:\opencv\build\common\tbb\intel64\vc10\ to your PATH system variable
  4. Download javacv-0.9-bin.zip
  5. Extract to C:\lib
  6. Add JavaCV Jars to Project’s build path

See below for more details.

Installing OpenCV

JavaCV is only a wrapper around OpenCV, so you have to have OpenCV first for anything to work. Sometimes it can be a huge pain installing libraries in a windows environment. Luckily there are working binaries for OpenCV. All you need to do is download and install them. The installer is really just a self-extracting zip file though, so once OpenCV has been extracted we need to add the bin/ folders to out PATH system variable, otherwise it wont be useable.

  1. Download OpenCV-2.3.1-win-superpack.exe
  2. Install to C:\
  3. Add C:\opencv\build\x64\vc10\bin;C:\opencv\build\common\tbb\intel64\vc10\ to your PATH system variable

If you use a newer version of OpenCV you might end up adding a different path. Most people didn’t have put C:\opencv\build\common\tbb\intel64\vc10\ on their PATH, but it seems very important for the version I have.

I also saw several people insisting that it was very important that OpenCV be installed to the root of your C drive. While I am pretty sure you should be able to move it around anywhere and it work, as long as your PATH is set correctly, after several hours debugging you tend to start dropping assumptions like that and taking safe routes. Extract OpenCV anywhere you want, but if you are having trouble, try moving it to C:\

Installing JavaCV

If you are a Maven fan, like myself, you might be tempted to just add a dependency to your prom file like so

<dependency>
  <groupId>org.bytedeco</groupId>
  <artifactId>javacv</artifactId>
  <version>0.9</version>
</dependency>

Their github page even suggests it saying “we can also have everything downloaded and installed automatically.” While this will get JavaCV set up with your java project, it unfortunately will not play so well with your OpenCV native libraries you installed in the last step. Or it didn’t for me. I could not find a way to get the maven dependency working. And I wanted it to work so badly.

Instead we will install the Jar files manually and add them to the projects build path.

  1. Download javacv-0.9-bin.zip
  2. Extract the zip file to anywhere, I chose C:\lib

Remember where you extract JavaCV, we will need this location when configuring the build path next.

Add JavaCV to your project’s build path in Eclipse

We need to point our project to the JavaCV jar files we extracted in the previous step. There are plenty of examples out there about how to do this so I wont go in to too much detail, but the basics in Eclipse are:

  1. Project > Properties > Java Build Path > Libraries > Add External JARs
  2. Navigate to the location of JavaCV on your file system, for me that is C:\lib\javacv-bin
  3. Add all the jars, yes all of them.

You should be good to go now. If not, you might need to read the next section.

java.lang.UnsatisfiedLinkError: no jniopencv_core in java.library.path

java.lang.UnsatisfiedLinkError: no jniopencv_core in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
	at java.lang.Runtime.loadLibrary0(Runtime.java:845)
	at java.lang.System.loadLibrary(System.java:1084)
	at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:535)
	at org.bytedeco.javacpp.Loader.load(Loader.java:410)
	at org.bytedeco.javacpp.Loader.load(Loader.java:353)
	at org.bytedeco.javacpp.opencv_core.<clinit>(opencv_core.java:10)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:266)
	at org.bytedeco.javacpp.Loader.load(Loader.java:385)
	at org.bytedeco.javacpp.Loader.load(Loader.java:353)
	at org.bytedeco.javacpp.opencv_highgui.<clinit>(opencv_highgui.java:13)
	at com.mmstratton.sandbox.javacv.Intro.intro(Intro.java:14)
...

If you are lucky your installation went perfectly and you will never see this error. If you are like me though, you will quickly grow to hate this little exception. I even went so far as to compile OpenCV from source, thinking there was something wrong with the prepackaged binaries.

watched an episode of the Office while it finished
And it still didnt work…

Basically this exception happens because JavaCV cant find OpenCV on your system. The most likely problems are:

  • OpenCV isnt in your PATH
  • Missing Microsoft Visual C++ redistributable package, you might need both 64-bit and 32-bit (there are mixed opinions)
  • JavaCV version not compatible with OpenCV version. Though I never found a list matching JavaCV versions to OpenCV versions, people seemed to think there could be compatibility issues. I used JavaCV 0.9 and OpenCV 2.3.1 together successfully

If you think everything is set up properly, and things still aren’t working…start over from scratch. I spent hours tinkering trying to get rid of this error. I finally got it all working together when I started over completely fresh. Follow the instructions very carefully.

Downloads

References

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>