如何从命令行运行包含 OpenCV 代码并使用 ant 创建的 .jar 文件? [英] How to run a .jar file, which contains OpenCV code and was created using ant, from command line?

查看:34
本文介绍了如何从命令行运行包含 OpenCV 代码并使用 ant 创建的 .jar 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Ubuntu 控制台运行一个小型 java 类,其中包含这样的 OpenCV 代码:

I am trying to run a small java class from Ubuntu console, which contains OpenCV code like this :

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;

class SimpleSample {

    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args) {
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
        System.out.println("OpenCV Mat: " + m);
        Mat mr1 = m.row(1);
        mr1.setTo(new Scalar(1));
        Mat mc5 = m.col(5);
        mc5.setTo(new Scalar(5));
        System.out.println("OpenCV Mat data:\n" + m.dump());
    }
}

我用蚂蚁构建了这个;build.xml 看起来像这样:-

I built this using ant; the build.xml looks like this:-

<project name="SimpleSample" basedir="." default="rebuild-run">

    <property name="lib.dir"     value="/opencv-2.4.7/build/bin"/>
    <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="/opencv-2.4.7/build/lib"/>
            <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>

当给定命令ant"时,代码产生以下输出:-

When given the command "ant", the code produces following output:-

Buildfile: /opencv-2.4.7/samples/java/ant/build.xml

clean:
    [delete] Deleting directory /opencv-2.4.7/samples/java/ant/build

compile:
    [mkdir] Created dir: /opencv-2.4.7/samples/java/ant/build/classes
    [javac] Compiling 1 source file to /opencv-2.4.7/samples/java/ant/build/classes

jar:
    [mkdir] Created dir: /opencv-2.4.7/samples/java/ant/build/jar
    [jar] Building jar: /opencv-2.4.7/samples/java/ant/build/jar/SimpleSample.jar

run:
    [java] Welcome to OpenCV 2.4.7.0
    [java] OpenCV Mat: Mat [ 5*10*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7fc23c1bbc90, dataAddr=0x7fc23c1bbd50 ]
    [java] OpenCV Mat data:
    [java] [0, 0, 0, 0, 0, 5, 0, 0, 0, 0;
    [java]   1, 1, 1, 1, 1, 5, 1, 1, 1, 1;
    [java]   0, 0, 0, 0, 0, 5, 0, 0, 0, 0;
    [java]   0, libdc1394 error: Failed to initialize libdc1394
    [java] 0, 0, 0, 0, 5, 0, 0, 0, 0;
    [java]   0, 0, 0, 0, 0, 5, 0, 0, 0, 0]

rebuild-run:

BUILD SUCCESSFUL
Total time: 3 seconds

但是当我尝试运行 ant 在上一步中生成的 .jar 文件时,使用命令 java -jar SimpleSample.jar,它给出了以下错误:-

But when I try to run the .jar file that was generated in the previous step by ant, using command java -jar SimpleSample.jar, it gives the following error :-

Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
    at SimpleSample.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

我打算从命令行本身运行 SimpleSample.jar 文件,没有任何错误.有人能告诉我我需要做什么才能做到这一点吗?

推荐答案

我通过将 OpenCV 库 jar 与我构建的 jar 打包来解决它.在 build.xml"jar" 目标中,我添加了一个 "zipgroupfileset" 属性如下:-

I solved it by packaging the OpenCV library jar with the jar I built. In the "jar" target of the build.xml, I added a "zipgroupfileset" attributed like this :-

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="/home/krozona/dev/programs/apache-tomcat-7.0.47/webapps/Krozona/WEB-INF/lib/${ant.project.name}.jar" basedir="${classes.dir}">
        <zipgroupfileset dir="${lib.dir}" includes="**/*.jar"/>
    </jar>
</target>

这篇关于如何从命令行运行包含 OpenCV 代码并使用 ant 创建的 .jar 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆