简体   繁体   English

Ant-TestNG类路径问题:getResource找不到文件

[英]Ant-TestNG classpath issue: getResource can't find the file

I got a TestNG test, that works perfectly fine on it's own. 我有一个TestNG测试,单独运行就可以很好地工作。 Somewhere in it I got: 在它的某个地方,我得到了:

URL fileName = this.getClass().getClassLoader().getResource("config.properties");

I'm trying to run the same test using Ant: 我正在尝试使用Ant运行相同的测试:

<testng outputDir="${resultsFolder}" haltOnFailure="false">
  <classpath>
      <fileset dir="./src/">
        <include name="**/*.properties"/>
      </fileset>
            -----------------------------...--------------------
 </classpath>
 <classfileset dir="correct path here" includes="**/*.class" />
</testng>

I can see in debug mode that the config.properties is in the classpath. 我可以在调试模式下看到config.properties在类路径中。 But the line at the top can not find it, it is null. 但是顶部的行找不到它,它为null。

EDIT: I solved it.The critical line actually does not search for the file in the classpath directly, it searches IN the files/folders. 编辑:我解决了。关键行实际上并不直接在类路径中搜索文件,而是在文件/文件夹中搜索。 So, this solved my problem: 因此,这解决了我的问题:

<pathelement location="src/"/>

Thanks for the help. 谢谢您的帮助。

try to replace your <classpath>...</classpath> with this: 尝试以此替换您的<classpath>...</classpath>

<classpath>
     <pathelement path="./src"/>
</classpath>

In order for JVM to find the config.properties the parent directory of config.properties should be in classpath. 为了让JVM找到config.properties的父目录config.properties应该在类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM