我在尝试运行包含OpenCV代码的jar文件时遇到问题我已经在我的ubuntu服务器上编译并运行ANT
。我所遵循的代码来自此处:http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html
所以我有一个简单的应用程序如下:
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
包含以下内容:
<project name="SimpleSample" basedir="." default="rebuild-run">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="/var/opencv/opencv/build/modules/java/pure_test/.build/simplesample/lib"/>
<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>
<path id="libraries.path">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="/var/opencv/opencv/build/modules/java/pure_test/.build/simplesample/lib/${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="/var/opencv/opencv/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
编译并运行上述内容时,我得到了正确的响应,并从文件执行中获取:
但是当我尝试使用java -jar SimpleSample.jar
运行jar文件时,我收到了一些错误。目前我得到这个no main manifest attribute, in SimpleSample.jar
,即使我确实有一个与jar相关联的清单属性..
在其他情况下,我发现了这样的错误,但我一直在玩build.xml
文件:
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
我已经在SO上查看了一些与此问题有关的问题,例如How to run a .jar file, which contains OpenCV code and was created using ant, from command line?这基本上与我遇到的问题相同,但我尝试的任何解决方案都没有用。