Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a native library path to the JUNIT task?

Tags:

java

junit

ant

I have a Java project that uses this driver for serial communication. The driver uses a dll under Windows to create serial ports.

The project contains several JUnit tests which complete successfully using the "Run as -> JUnit Test". However, the tests referencing the native library fail when running ant (and tests that do not reference the native library pass).

My best guess so far is to add the directory that contains the native library to the java.library.path, but I haven't succeeded in doing so through the build.xml file.

Can somebody tell a (clean) solution?

Here is my build.xml:

<path id="compile.classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${junit_home}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<path id="test.classpath">
    <pathelement location="${bin}" />
    <fileset dir="${lib}">
         <include name="**/*.jar"/>
     </fileset>
    <fileset dir="${junit_home}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<target name="compile">
    <mkdir dir="${bin}" />
    <echo Message="Compiling src folder..." />
    <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${src}" destdir="${bin}" />
    <echo Message="Compiling test folder..." />
    <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${test}" destdir="${bin}" />
</target>

<target name="test">
    <mkdir dir="${test.reports}" />
    <junit fork="yes" printsummary="yes" haltonfailure="yes">
        <test name="${test.class.name}" todir="${test.reports}" />
        <formatter type="xml" />
        <classpath refid="test.classpath" />
    </junit>
</target>

And here is a part of the test report (in XML):

    <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testGetInstance" time="0.0" />
  <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateDefaultComport" time="0.016">
    <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
    at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method)
    at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    at package.comport.GioComport.getFreeSerialPorts(Unknown Source)
    at package.comport.GioComport.findDevice(Unknown Source)
    at package.comport.GioComport.&lt;init&gt;(Unknown Source)
    at package.comport.ComportFactory.createNewPort(Unknown Source)
    at package.comport.ComportFactory.createComport(Unknown Source)
    at package.comport.test.buildservertests.ComportFactoryTest.testCreateDefaultComport(Unknown Source)
</error>
  </testcase>
  <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateComportWithWrongSettings" time="0.0">
    <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
    at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method)
    at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    at package.comport.GioComport.getFreeSerialPorts(Unknown Source)
    at package.comport.GioComport.findDevice(Unknown Source)
    at package.comport.GioComport.&lt;init&gt;(Unknown Source)
    at package.comport.ComportFactory.createNewPort(Unknown Source)
    at package.comport.ComportFactory.createComport(Unknown Source)
    at package.comport.test.buildservertests.ComportFactoryTest.testCreateComportWithWrongSettings(Unknown Source)
</error>
  </testcase>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[java.lang.UnsatisfiedLinkError: no libSerialPort in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
like image 270
Timo Avatar asked Jun 10 '11 09:06

Timo


People also ask

What is Native library location in eclipse?

Go to Project properties->Java Build Path->Source. You'll find a list of source-folders. Each entry under the the Source tab has Native library locations.

What is Djava library path?

java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project.


2 Answers

The junit task in Ant, allow setting system properties, just like some of the other tasks. You'll need to specify the java.library.path value in the sysproperty nested element as:

<junit fork="yes" printsummary="yes" haltonfailure="yes">
    <test name="${test.class.name}" todir="${test.reports}" />
    <formatter type="xml" />
    <classpath refid="test.classpath" />
    <sysproperty key="java.library.path" value="put your library path here"/>
</junit>
like image 136
Vineet Reynolds Avatar answered Oct 18 '22 16:10

Vineet Reynolds


Use a jvmarg to set the library load path:

<junit>
  <jvmarg value="-Djava.library.path=/blah/YOURPATH"/>

If you want to add your directory to the existing path, you'll need to use Ant's ability to use environment variables:

<property environment="env"/>
<junit>
  <jvmarg value="-Djava.library.path=${env.path}${path.separator}/blah/PATH"/>
like image 27
Nick Fortescue Avatar answered Oct 18 '22 15:10

Nick Fortescue