Java 运行 ant build 给出“包 org.junit 不存在”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792445/
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
Running ant build gives "package org.junit does not exist"
提问by Liron Yahdav
When I use a Java IDE to build projects (e.g. NetBeans) that have JUnit tests, they compile fine, but when I try to use ant outside of the IDE to run the build script, I get the error "package org.junit does not exist".
当我使用 Java IDE 构建具有 JUnit 测试的项目(例如 NetBeans)时,它们编译得很好,但是当我尝试在 IDE 之外使用 ant 来运行构建脚本时,出现错误“package org.junit does not存在”。
采纳答案by DJ.
You should add your junit.jar into the classpath definition in your ant file.
您应该将 junit.jar 添加到 ant 文件中的类路径定义中。
There are many way to do it, one example is:
有很多方法可以做到,一个例子是:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="your.classpath.refid" />
<fileset dir="${junit.dir}">
<include name="**/junit.jar" />
</fileset>
</classpath>
...
</junit>
See Ant Manualfor details on setting up your classpath.
有关设置类路径的详细信息,请参阅Ant 手册。
回答by Liron Yahdav
The problem was that in the IDE, it set the classpath correctly to include the .jar for JUnit. Running ant outside the IDE, the classpath was different, thus the error. The fix was to put the JUnit .jar in the folder "C:\Program Files\Java\jre6\lib\ext" so it would always be found outside of any IDE.
问题是在 IDE 中,它正确设置了类路径以包含 JUnit 的 .jar。在 IDE 外运行 ant,类路径不同,因此出现错误。修复方法是将 JUnit .jar 放在文件夹“C:\Program Files\Java\jre6\lib\ext”中,这样它就可以在任何 IDE 之外找到。
回答by NotAgain says Reinstate Monica
Late answer here.
迟到的答案在这里。
Copy the junit.jar file to the ${ANT_HOME}/lib folder.
将 junit.jar 文件复制到 ${ANT_HOME}/lib 文件夹。