Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向JUNIT任务添加本机库路径?_Java_Ant_Junit - Fatal编程技术网

Java 如何向JUNIT任务添加本机库路径?

Java 如何向JUNIT任务添加本机库路径?,java,ant,junit,Java,Ant,Junit,我有一个用于串行通信的Java项目。驱动程序使用Windows下的dll创建串行端口 该项目包含几个JUnit测试,它们使用“RunAs->JUnit测试”成功完成。但是,在运行ant时,引用本机库的测试失败(以及不引用本机库的测试) 到目前为止,我最好的猜测是将包含本机库的目录添加到java.library.path中,但我还没有通过build.xml文件成功地实现这一点 有人能说出(干净的)解决办法吗 以下是我的build.xml: <path id="compile.classpat

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

该项目包含几个JUnit测试,它们使用“RunAs->JUnit测试”成功完成。但是,在运行ant时,引用本机库的测试失败(以及不引用本机库的测试)

到目前为止,我最好的猜测是将包含本机库的目录添加到java.library.path中,但我还没有通过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>

以下是测试报告的一部分(XML):


java.lang.UnsatisfiedLinkError:giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
在giovynet.nativelink.SerialPort.getStateSerialPortC(本机方法)
在giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
位于package.comport.GioComport.getFreeSerialPorts(未知源)
位于package.comport.GioComport.findDevice(未知源)
位于package.comport.giocomort.init(未知源)
位于package.comport.ComportFactory.createNewPort(未知源)
位于package.comport.ComportFactory.createComport(未知源)
位于package.comport.test.buildservertests.ComportFactoryTest.testCreateDefaultComport(未知源)
java.lang.UnsatisfiedLinkError:giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
在giovynet.nativelink.SerialPort.getStateSerialPortC(本机方法)
在giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
位于package.comport.GioComport.getFreeSerialPorts(未知源)
位于package.comport.GioComport.findDevice(未知源)
位于package.comport.giocomort.init(未知源)
位于package.comport.ComportFactory.createNewPort(未知源)
位于package.comport.ComportFactory.createComport(未知源)
位于package.comport.test.buildservertests.ComportFactoryTest.TestCreateComportWithErrorSettings(未知源)
使用设置库加载路径:

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

如果要将目录添加到现有路径,则需要使用:


与其他一些任务一样,允许设置系统属性。您需要将
sysproperty
嵌套元素中的
java.library.path
值指定为:

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


谢谢你的回答!这会取代路径,对吗?如何将我的目录添加到路径(path=path+new dir;)@Timo,您可能会发现该任务在这方面很有用。也请参见。
这也可以吗?谢谢你的回答!这会取代路径,对吗?如何将我的目录添加到路径(path=path+newdir;))抱歉,响应太慢。如果您还没有找到它,我已经将其添加到回答中。应该注意,该解决方案需要属性fork=“yes”!
<property environment="env"/>
<junit>
  <jvmarg value="-Djava.library.path=${env.path}${path.separator}/blah/PATH"/>
<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>