java Ant-JUnit 错误:Ant 想要在它的类路径中使用 JUnit .jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4809695/
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
Ant-JUnit Error: Ant wants the JUnit .jar in it's classpath
提问by stefina
Hi guys, I am a bit new to JUnit and Ant. I want to know what this error means:
大家好,我对 JUnit 和 Ant 有点陌生。我想知道这个错误是什么意思:
The <classpath> for <junit> must include junit.jar if not in Ant's own classpath
I am compiling a Java project, and I cannot get beyond this point.
我正在编译一个 Java 项目,我无法超越这一点。
回答by Kevin Stembridge
The documentation of the Junit ant task gives a list of options for how to get junit.jar onto the classpath:
Junit ant 任务的文档提供了如何将 junit.jar 放入类路径的选项列表:
http://ant.apache.org/manual/Tasks/junit.html
http://ant.apache.org/manual/Tasks/junit.html
To save you the lookup, the options are reproduced below. My preference is option 1.
为了节省您的查找时间,下面复制了这些选项。我的偏好是选项 1。
- Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
- Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
- Add both JARs to your classpath using -lib.
- Specify the locations of both JARs using a element in a in the build file.
- Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the passed to . (since Ant 1.7)
- 将 junit.jar 和 ant-junit.jar 都放在 ANT_HOME/lib 中。
- 不要将它们放在 ANT_HOME/lib 中,而是将它们的位置包含在 CLASSPATH 环境变量中。
- 使用 -lib 将这两个 JAR 添加到您的类路径中。
- 使用构建文件中的元素指定两个 JAR 的位置。
- 将 ant-junit.jar 保留在 ANT_HOME/lib 中的默认位置,但在传递给 . (从 Ant 1.7 开始)
回答by Edgard Leal
<property name="lib.dir" value="webcontent\WEB-INF\lib" />
<path id="classPath">
<pathelement location="${lib.dir}/junit-4.11.jar" />
</path>
<target name="test" depends="build">
<junit haltonerror="false" haltonfailure="false" fork="yes">
<classpath path="${lib.dir}">
<path refid="classPath" />
</classpath>
</junit>
</target>
回答by CharlesTBetz
I spent a couple hours with this problem today. I had the .jar files all specified in Eclipse via Project|Properties|Java Build Path, but was still getting the
我今天花了几个小时解决这个问题。我通过 Project|Properties|Java Build Path 在 Eclipse 中指定了所有 .jar 文件,但仍然得到
<classpath> for <junit> must include junit.jar if not in Ant's own classpath
error when running Ant from Eclipse.
从 Eclipse 运行 Ant 时出错。
Running Ant from the command line would work fine (I had everything in the classpath environment variable).
从命令行运行 Ant 可以正常工作(我在类路径环境变量中拥有所有内容)。
But in Eclipse the only thing that worked was to explicitly state the classpath inside the elements, e.g.:
但在 Eclipse 中,唯一有效的是在元素内显式声明类路径,例如:
<path id="JUnit 4.libraryclasspath">
<pathelement location="...\plugins\org.junit_4.11.0.v201303080030\junit.jar"/>
<pathelement location="...\plugins\org.hamcrest.core_1.3.0.v201303031735.jar"/>
<pathelement location="...\lib\ant-junit4.jar"/>
</path>
<path id="Ant1.classpath">
<pathelement location="bin"/>
<pathelement location="."/>
<path refid="JUnit 4.libraryclasspath"/>
</path>
... stuff...
<target name="test1" depends="compile">
<junit>
<classpath refid="Ant1.classpath"/>
</junit>
</target>
Without explicitly specifying the classpath within the junit element, it would break in eclipse every time, even just a bare
如果没有在 junit 元素中明确指定类路径,它每次都会在 eclipse 中中断,即使只是一个裸露的
<junit/>
reference
参考
I'm no expert, just reporting what worked today.
我不是专家,只是报告今天的工作。
-ctb
-ctb
回答by Luc
I have no clue what it means, but in my case it seems there was a conflict because of Dropbox. Restarting Netbeans resolved the issue. Might have something to do with my using Linux and classmates using Windows, but I'm not sure.
我不知道这意味着什么,但就我而言,似乎由于 Dropbox 存在冲突。重新启动 Netbeans 解决了该问题。可能与我使用 Linux 和使用 Windows 的同学有关,但我不确定。
"Just restart Netbeans" might be too simple for a stackoverflow answer, but if someone had posted it, it would have saved me some time...
“只需重新启动 Netbeans”对于 stackoverflow 答案来说可能太简单了,但是如果有人发布了它,它会为我节省一些时间......
回答by DwB
I believe that the following is the cause: junit.jar is not in your CLASSPATH environment variable. Either add junit.jar to your CLASSPATH or add it to the classpath that you define in your ant build file.
我认为原因如下:junit.jar 不在您的 CLASSPATH 环境变量中。将 junit.jar 添加到您的 CLASSPATH 或将其添加到您在 ant 构建文件中定义的类路径。
Here is an introduction to ant.
回答by Seyfettin ???EK
Window -> Preferences -> Ant -> Runtime -> Classpath Add junit-xx.jar to the Global Entries as an external jar file.
Window -> Preferences -> Ant -> Runtime -> Classpath 将 junit-xx.jar 作为外部 jar 文件添加到 Global Entries 中。