title: How to add a native library path to the JUNIT task?
body: <p>I have a Java project that uses <a href="http://java.giovynet.com/Giovynet/">this driver</a> for serial communication. The driver uses a dll under Windows to create serial ports. </p>
  
  <p>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).</p>
  
  <p>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. </p>
  
  <p>Can somebody tell a (clean) solution?</p>
  
  <p>Here is my build.xml:</p>
  
  <pre><code>&lt;path id="compile.classpath"&gt;
      &lt;fileset dir="${lib}"&gt;
          &lt;include name="**/*.jar"/&gt;
      &lt;/fileset&gt;
      &lt;fileset dir="${junit_home}"&gt;
          &lt;include name="**/*.jar"/&gt;
      &lt;/fileset&gt;
  &lt;/path&gt;
  
  &lt;path id="test.classpath"&gt;
      &lt;pathelement location="${bin}" /&gt;
      &lt;fileset dir="${lib}"&gt;
           &lt;include name="**/*.jar"/&gt;
       &lt;/fileset&gt;
      &lt;fileset dir="${junit_home}"&gt;
          &lt;include name="**/*.jar"/&gt;
      &lt;/fileset&gt;
  &lt;/path&gt;
  
  &lt;target name="compile"&gt;
      &lt;mkdir dir="${bin}" /&gt;
      &lt;echo Message="Compiling src folder..." /&gt;
      &lt;javac includeantruntime="no" classpathref="compile.classpath" srcdir="${src}" destdir="${bin}" /&gt;
      &lt;echo Message="Compiling test folder..." /&gt;
      &lt;javac includeantruntime="no" classpathref="compile.classpath" srcdir="${test}" destdir="${bin}" /&gt;
  &lt;/target&gt;
  
  &lt;target name="test"&gt;
      &lt;mkdir dir="${test.reports}" /&gt;
      &lt;junit fork="yes" printsummary="yes" haltonfailure="yes"&gt;
          &lt;test name="${test.class.name}" todir="${test.reports}" /&gt;
          &lt;formatter type="xml" /&gt;
          &lt;classpath refid="test.classpath" /&gt;
      &lt;/junit&gt;
  &lt;/target&gt;
  </code></pre>
  
  <p>And here is a part of the test report (in XML):</p>
  
  <pre><code>    &lt;testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testGetInstance" time="0.0" /&gt;
    &lt;testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateDefaultComport" time="0.016"&gt;
      &lt;error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError"&gt;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.&amp;lt;init&amp;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)
  &lt;/error&gt;
    &lt;/testcase&gt;
    &lt;testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateComportWithWrongSettings" time="0.0"&gt;
      &lt;error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError"&gt;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.&amp;lt;init&amp;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)
  &lt;/error&gt;
    &lt;/testcase&gt;
    &lt;system-out&gt;&lt;![CDATA[]]&gt;&lt;/system-out&gt;
    &lt;system-err&gt;&lt;![CDATA[java.lang.UnsatisfiedLinkError: no libSerialPort in java.library.path
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
  </code></pre>