- OpenCV 3.0 Computer Vision with Java
- Daniel Lélis Baggio
- 219字
- 2021-07-16 13:17:31
Building your project with Ant
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:
<project name="SimpleSample" basedir="." default="rebuild-run"> <property name="src.dir" value="src"/> <property name="lib.dir" value="${ocvJarDir}"/> <path id="classpath"> <fileset dir="${lib.dir}" includes="**/*.jar"/> </path> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="${ant.project.name}"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java fork="true" classname="${main-class}"> <sysproperty key="java.library.path" path="${ocvLibDir}"/> <classpath> <path refid="classpath"/> <path location="${jar.dir}/${ant.project.name}.jar"/> </classpath> </java> </target> <target name="rebuild" depends="clean,jar"/> <target name="rebuild-run" depends="clean,run"/> </project>
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"/>
.
- UNIX編程藝術
- 深度實踐OpenStack:基于Python的OpenStack組件開發
- 在最好的年紀學Python:小學生趣味編程
- Photoshop智能手機APP UI設計之道
- JavaFX Essentials
- Django開發從入門到實踐
- 匯編語言程序設計(第2版)
- Python神經網絡項目實戰
- 3D少兒游戲編程(原書第2版)
- HDInsight Essentials(Second Edition)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- 微服務架構深度解析:原理、實踐與進階
- Scala Data Analysis Cookbook
- Managing Microsoft Hybrid Clouds
- Python編程快速上手2