java 如何将本机库路径添加到 JUNIT 任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6304483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:17:05  来源:igfitidea点击:

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

javaantjunit

提问by Timo

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

我有一个使用此驱动程序进行串行通信的 Java 项目。该驱动程序使用 Windows 下的 dll 来创建串行端口。

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).

该项目包含多个 JUnit 测试,这些测试使用“运行方式 -> JUnit 测试”成功完成。但是,在运行 ant 时,引用本机库的测试会失败(以及不引用本机库 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.

到目前为止,我最好的猜测是将包含本机库的目录添加到 java.library.path,但我没有通过 build.xml 文件成功地这样做。

Can somebody tell a (clean) solution?

有人可以告诉一个(干净的)解决方案吗?

Here is my build.xml:

这是我的 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):

这是测试报告的一部分(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)

回答by Vineet Reynolds

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

Ant 中junit 任务允许设置系统属性,就像其他一些任务一样。您需要java.library.pathsysproperty嵌套元素中的值指定为:

<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>

回答by Nick Fortescue

Use a jvmargto set the library load path:

使用jvmarg设置库加载路径:

<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:

如果要将目录添加到现有路径,则需要使用Ant 使用环境变量的能力

<property environment="env"/>
<junit>
  <jvmarg value="-Djava.library.path=${env.path}${path.separator}/blah/PATH"/>