1

I have a openCV project in eclipse. Now I am trying to make it runnable Jar but not able to launch it , once trying to run the jar. I tried following - https://groups.google.com/forum/#!topic/javacv/ziqKIb7PgYk

but I couldn't understand properly. Can anyone explain the proper way to do the needful.

in my System , OpenCV is installed. Once I try to run the project from eclipse , everything works fine . but when I try to do the same from runnabelJar it doesn't. The problem I found it that I didn't include .dll files , so how should I do it.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

2 Answers2

2

I finally got the solution. Yes we can easily make Runnable jars from eclipse while using javaCV , no need for ant or maven build.

No Need to set the class path with OpenCV, which you are only using javaCV and no intention for jni build.

  • Create a Java Project.
  • Copy all the jars.You can avoid x86 or linux or non required jars as well.

enter image description here

  • Find the jre version , if 32 bit , get the path of OpenCV 32 bits libraries else 64 bits. You can place those dlls any where in the system.. No need to place it any specific path.
  • Go to OpenCV jar , expand it , click on Native Library Location , edit and browse the path

enter image description here

Thats it , job Done , now make Runnable jar or anything , it will work like charm.

Once you provide your runnable jar to any user, then either user's openCV path should be set in the class path or you provide the dlls , and you Load the library in any static block.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
0

Building a jar with ANT (for javacv 0.9):

  1. install opencv (and/or ffmpeg)

  2. download javacv from https://github.com/bytedeco/javacv

  3. Create a Java Project and create the directories 'lib', 'lib32' and 'lib64' in the root and add the x64 jars from javacv to lib64 - same for lib32 and the others without bit-version in lib

  4. Add the following jars to the buildpaht of your project (Add External JARs...) and set the Native library paths for opencv (and/or ffmpeg)

enter image description here

  1. Sample Test class
public class Main { 
    public static void main(String[] args) throws Exception, IOException {
        OpenCVFrameGrabber frameGrabber = new OpenCVFrameGrabber(new File(".\\tmp_files\\small.mp4"));
        frameGrabber.start();
        IplImage origImg = frameGrabber.grab();

        //create canvas frame named 'Demo'
        final CanvasFrame canvas = new CanvasFrame("Demo");

        //Show image in canvas frame
        canvas.showImage(origImg);

        //This will close canvas frame on exit
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);    
    }
}
  1. ANT -script
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="main" name="Main">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <property name="build.dir"     value="bin"/>
    <property name="src.dir"       value="src"/>
    <property name="lib.dir"       value="lib"/> 
    <property name="lib32.dir"       value="lib32"/>
    <property name="lib64.dir"       value="lib64"/>
    <property name="classes.dir"   value="${build.dir}/classes"/>
    <property name="jar.dir"       value="${build.dir}/jar"/>
    <property name="jar.file"      value="${jar.dir}/buildOpenCvTest32_64.jar"/>
    <property name="manifest.file" value="${jar.dir}/MANIFEST.MF"/>

    <property name="main.class" value="test.Main"/>

    <path id="external.jars">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <fileset dir="${libV.dir}" includes="**/*.jar"/>
    </path>

    <path id="project.classpath">
        <pathelement location="${src.dir}"/>
        <path refid="external.jars" />
    </path>

    <target name="init">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${jar.dir}"/>
        <copy includeemptydirs="false" todir="${build.dir}">
            <fileset dir="${src.dir}">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="cleanall" depends="clean"/>

    <target name="build" depends="init">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}" classpathref="project.classpath">
            <src path="${src.dir}"/>
        </javac>
    </target>

    <target name="build-jar" depends="build">
        <delete file="${jar.file}" />
        <delete file="${manifest.file}" />

        <manifest file="${manifest.file}" >
            <attribute name="built-by" value="${user.name}" />
            <attribute name="Main-Class" value="${main.class}" />
        </manifest>

        <jar destfile="${jar.file}" 
            basedir="${build.dir}" 
            manifest="${manifest.file}">
            <fileset dir="${classes.dir}" includes="**/*.class" />
            <zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
            <zipgroupfileset dir="${lib32.dir}" includes="**/*.jar" />
            <zipgroupfileset dir="${lib64.dir}" includes="**/*.jar" />
        </jar>

    </target>

    <target name="main" depends="build-jar">
        <description>Main target</description>
      </target>
</project>
  1. Run ANT - the jar should run on every 32 and 64bit Windows system. same should work for ffmpeg library and for unix systems
Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35