title: How to run a .jar file, which contains OpenCV code and was created using ant, from command line?
body: <p>I am trying to run a small java class from Ubuntu console, which contains OpenCV code like this :</p>
  
  <pre><code>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());
      }
  }
  </code></pre>
  
  <p>I built this using ant; the build.xml looks like this:-</p>
  
  <pre><code>&lt;project name="SimpleSample" basedir="." default="rebuild-run"&gt;
  
      &lt;property name="lib.dir"     value="/opencv-2.4.7/build/bin"/&gt;
      &lt;path id="classpath"&gt;
          &lt;fileset dir="${lib.dir}" includes="**/*.jar"/&gt;
      &lt;/path&gt;
  
      &lt;property name="build.dir"   value="build"/&gt;
      &lt;property name="classes.dir" value="${build.dir}/classes"/&gt;
      &lt;property name="jar.dir"     value="${build.dir}/jar"/&gt;
  
      &lt;property name="main-class"  value="${ant.project.name}"/&gt;
  
  
      &lt;target name="clean"&gt;
          &lt;delete dir="${build.dir}"/&gt;
      &lt;/target&gt;
  
      &lt;target name="compile"&gt;
          &lt;mkdir dir="${classes.dir}"/&gt;
          &lt;javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/&gt;
      &lt;/target&gt;
  
      &lt;target name="jar" depends="compile"&gt;
           &lt;mkdir dir="${jar.dir}"/&gt;
           &lt;jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"&gt;
               &lt;manifest&gt;
                   &lt;attribute name="Main-Class" value="${main-class}"/&gt;
               &lt;/manifest&gt;
           &lt;/jar&gt;
      &lt;/target&gt;
  
      &lt;target name="run" depends="jar"&gt;
          &lt;java fork="true" classname="${main-class}"&gt;
              &lt;sysproperty key="java.library.path" path="/opencv-2.4.7/build/lib"/&gt;
              &lt;classpath&gt;
                  &lt;path refid="classpath"/&gt;
                  &lt;path location="${jar.dir}/${ant.project.name}.jar"/&gt;
              &lt;/classpath&gt;
          &lt;/java&gt;
      &lt;/target&gt;
  
      &lt;target name="rebuild" depends="clean,jar"/&gt;
  
      &lt;target name="rebuild-run" depends="clean,run"/&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>When given the command "ant", the code produces following output:-</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But when I try to run the .jar file that was generated in the previous step by ant, using command <code>java -jar SimpleSample.jar</code>, it gives the following error :-</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
      at SimpleSample.&lt;clinit&gt;(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
  </code></pre>
  
  <p><strong>I intend to run the SimpleSample.jar file from command line itself, without any error. Can someone tell me what do I need to do to accomplish that?</strong></p>