Forum Stats

  • 3,826,768 Users
  • 2,260,705 Discussions
  • 7,897,072 Comments

Discussions

Loading a dll from a jar using JNI

AnjanN
AnjanN Member Posts: 19
edited Aug 30, 2013 1:41PM in Java Native Interface (JNI)

Hi,

I am quite new to JNI. I developed a small program in JNI that loads a dll and invokes a method in that dll. Below is the code snippet.

package JNI;

public class StringWrapper {

    public native String getTransformedString(String strString);

    static

    {

        try{

            System.out.println("Trying to load StringWrapper.dll with System.loadLibrary().........");

            System.loadLibrary("dll//StringWrapper");}

        catch (Throwable ex){

            ex.printStackTrace();}

    }

    public static String getTransformedString(String strString){

        String strTransformedString = "";

        try{

            strTransformedString = new StringWrapper().getTransformedString(strString);}

        catch (Exception ex){

            ex.printStackTrace();}

        return strTransformedString;

    }}

I am calling this static method 'getTransformedString(String)' from a main class and build the following with ant tool.My package hierarchy and build.xml is as follows :

Root

   |

   lib

   |

   src

   ||

   |- MainClass.java

   |+ PACKAGE JNI

   |     |

   |     + dll  (Inside dll folder exists the dll to be loaded with JNI i.e StringWrapper.dll)

   |     - StringWrapper.java

   |

   build.xml

   runJar.bat

build.xml

----------

<project name="StringWrapper" default="compile" basedir=".">

    <target name="init">

        <tstamp/>

        <property name="lib" value="lib"/>

        <property name="src" value="src"/>

        <property name="JNI" value="JNI"/>

        <property name="dll" value="dll"/>

        <property name="classes" value="classes"/>

        <property name="headers" value="headers"/>

        <property name="deploypath" value="."/>

        <property name="mainclass" value="JCStringTransformMain"/>

        <path id="client.build.classpath">

            <pathelement location="."/>

            <pathelement location="${classes}"/>

        </path>

    </target>

    <target name="prepare" depends="init">

        <mkdir dir="${classes}"/>

    </target>

   

    <target name="compile" depends="prepare">

        <javac destdir="${classes}" debug="on" deprecation="off" fork="yes" memoryInitialSize="32m" memoryMaximumSize="96m">

            <classpath refid="client.build.classpath"/>

            <src path="${src}"/>

        </javac>

<!--         <javah destdir="${headers}">

            <class name="JCStringTransformMain"/>

            <class name="JNI.StringWrapper"/>

        </javah> -->

    </target>

    <target name="copydll" description="copy dll">

        <copy toDir="${classes}/${JNI}/${dll}" overwrite="true">

            <fileset dir="${src}/${JNI}/dll">

                <include name="**/*.dll"/>

            </fileset>

        </copy>

    </target>

     <target name="deployJar" depends="compile,copydll">

<!--     <target name="deployJar" depends="copydll"> -->

        <jar destfile="${lib}/StringWrapper.jar" basedir="${classes}" includes="**">

            <manifest>

                <attribute name="Class-Path" value="${deploypath}"/>

                <attribute name="Main-Class" value="${mainclass}"/>

            </manifest>

        </jar>

        <delete dir="${src}/classes"/>

    </target>

    <target name="all" depends="deployJar"/>

</project>

My requirement is to make a jar file that can be used in any application where user can transform any string by calling the method. For this reason I copied the dll inside

another folder in the jar file. But after building the jar and running the application I encountered with an strange frustrating error :

Trying to load StringWrapper.dll with System.loadLibrary().........

java.lang.UnsatisfiedLinkError: no dll//StringWrapper in java.library.path

I just cannot find out where to provide the 'java.library.path' and what changes to be done and what path to be set in build xml to mke the jar run properly, so that when anybody

takes this jar file and calls the method he/she can transform a String and get the desired result.

After some explore I found that there is something related to path in the "System.loadLibrary()" method. So what exactly is the problem in my build.xml that

prevents JVM from loading the library, when I am attempting to load the dll from inside the jar.

Tagged:

Answers

This discussion has been closed.