If you want to rely on Apache Ant for building instead of using an IDE, a build.xml file is provided in the OpenCV samples. You can find this file in this chapter's repository as well. The following are its contents:
This is a basic build.xml Ant file that defines tasks such as cleaning, compiling, and packing a .jar file, running, rebuilding, and rebuild-running. It expects your source code to be in a sibling folder called src. Make sure that the SimpleSample.java source code provided earlier is inside this directory.
Compiling and running the project using Ant is easy. Simply type the following:
ant -DocvJarDir=path/to/dir/containing/opencv-300.jar -DocvLibDir=path/to/dir/containing/opencv_java300/native/library
In case you have downloaded and extracted pre-built binaries, use the following command instead:
ant -DocvJarDir=X:\opencv3.0.0\opencv\build\java -DocvLibDir=X:\opencv3.00\opencv\build\java\x64
A successful run of Ant build.xml will look like the following screenshot:
The provided build.xml file can be reused for building your Java OpenCV applications. In order to use it, make sure that the project name matches your main class name. If your main class is inside the package com.your.company, and it's called MainOpenCV, you should change the first line of build.xml from <project name="SimpleSample" basedir="." default="rebuild-run"> to <project name="com.your.company.MainOpenCV" basedir="." default="rebuild-run">.
You can also hardcode the ocvJarDir and ocvLibDir properties so you won't have to type them while invoking Ant. For ocvJarDir, simply change the <property name="lib.dir" value="${ocvJarDir}"/> command to <property name="lib.dir" value="X:\opencv2.47\opencv\build\java"/>.