Java Ant + JUnit:NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1171264/
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: NoClassDefFoundError
提问by SteveT
Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped.
好吧,我很沮丧!我已经四处寻找了好几个小时,但仍然被难住了。
Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins).
环境:WinXP、Eclipse Galileo 3.5(直接安装 - 无需额外插件)。
So, I have a simple JUnit test. It runs fine from it's internal Eclipse JUnit run configuration. This class has no dependencies on anything. To narrow this problem down as much as possible it simply contains:
所以,我有一个简单的 JUnit 测试。它从它的内部 Eclipse JUnit 运行配置运行良好。这个类不依赖任何东西。为了尽可能缩小这个问题,它只包含:
@Test
public void testX() {
assertEquals("1", new Integer(1).toString());
}
No sweat so far. Now I want to take the super advanced step of running this test case from within Ant (the final goal is to integrate with Hudson).
到现在都没有出汗。现在我想在 Ant 中运行这个测试用例的超级步骤(最终目标是与 Hudson 集成)。
So, I create a build.xml:
所以,我创建了一个 build.xml:
<project name="Test" default="basic">
<property name="default.target.dir" value="${basedir}/target" />
<property name="test.report.dir" value="${default.target.dir}/test-reports" />
<target name="basic">
<mkdir dir="${test.report.dir}" />
<junit fork="true" printSummary="true" showOutput="true">
<formatter type="plain" />
<classpath>
<pathelement path="${basedir}/bin "/>
</classpath>
<batchtest fork="true" todir="${test.report.dir}" >
<fileset dir="${basedir}/bin">
<include name="**/*Test.*" />
</fileset>
</batchtest>
</junit>
</target>
</project>
${basedir} is the Java project name in the workspace that contains the source, classes and build file. All .java's and the build.xml are in ${basedir}/src. The .class files are in ${basedir}/bin.
${basedir} 是包含源、类和构建文件的工作区中的 Java 项目名称。所有 .java 和 build.xml 都在 ${basedir}/src 中。.class 文件位于 ${basedir}/bin 中。
I have added eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar to the Ant Runtime Classpath via Windows / Preferences / Ant / Runtime / Contributed Entries. ant-junit.jar is in Ant Home Entries.
我已经通过 Windows / Preferences / Ant / Runtime / Contributed Entries 将 eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar 添加到 Ant 运行时类路径。ant-junit.jar 在 Ant Home Entries 中。
So, what happens when I run this insanely complex target? My report file contains:
那么,当我运行这个极其复杂的目标时会发生什么?我的报告文件包含:
Testsuite: com.xyz.test.RussianTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Testcase: initializationError took 0 sec
Caused an ERROR
org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access[junit] Implicitly adding /usr/share/ant/lib/junit4.jar:...
0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
What is this org.hamcrest.SelfDescribing class? Something to do with mocks? OK, fine. But why the dependency? I'm not doing anything at all with it. This is literally a Java project with no dependencies other than JUnit.
这个 org.hamcrest.SelfDescribing 类是什么?和mock有关系吗?好的。但为什么是依赖?我根本没有用它做任何事情。这实际上是一个 Java 项目,除了 JUnit 之外没有任何依赖项。
Stumped (and frustrated)!!
难倒(和沮丧)!
回答by dfa
add also hamcrest.jar to your testing classpath
将 hamcrest.jar 添加到您的测试类路径中
回答by tddmonkey
JUnit 4.5 includes Hamcrest as part of its matching system. You'll want to ensure you've got that JAR file on your classpath too
JUnit 4.5 将 Hamcrest 作为其匹配系统的一部分。你需要确保你的类路径上也有那个 JAR 文件
回答by kgiannakakis
The class you are missing is included in junit from version 4.4 and on (link).
您缺少的类包含在 4.4 版及以后的 junit 中(链接)。
Check if an older version of jUnit is in your classpath. Try running the ant script in a command shell to see if you get the same error. If you do, then probably ant uses a jUnit version older than 4.4
检查您的类路径中是否存在旧版本的 jUnit。尝试在命令 shell 中运行 ant 脚本,看看是否出现相同的错误。如果你这样做,那么可能 ant 使用的 jUnit 版本早于 4.4
Update:
更新:
Look at the ANT_HOME folder. Right click a build.xml file, select the second "Ant build" at the Run As.. dialog. A dialog for editing the ant run configuration will open (There might be an easier way to open this). Go to the Classpath tab. Add the newest junit at the classpath (lib folder of ant).
查看 ANT_HOME 文件夹。右键单击 build.xml 文件,在 Run As.. 对话框中选择第二个“Ant build”。将打开一个用于编辑 ant run 配置的对话框(可能有更简单的方法来打开它)。转到类路径选项卡。在类路径(ant 的lib 文件夹)添加最新的junit。
回答by akf
I dont think it is required to add the JUnit jar to your Ant classpath. Instead, check that you have the JUnit library in your Java Build Path (Windows/Preferences/Java/Build Path/Classpath Variables).
我认为不需要将 JUnit jar 添加到您的 Ant 类路径。相反,请检查您的 Java 构建路径(Windows/Preferences/Java/Build Path/Classpath Variables)中是否有 JUnit 库。
回答by SteveT
OK. I finally worked around this issue. I won't say "solved" since I am still quite confused as to what's going on. Anyway...
好的。我终于解决了这个问题。我不会说“已解决”,因为我仍然对发生的事情感到困惑。反正...
I downloaded the latest Ant (1.7.1) and JUnit (4.6) and exploded them into an external directory - NOT in the workspace or "in" Eclipse in any way.
我下载了最新的 Ant (1.7.1) 和 JUnit (4.6) 并将它们分解到一个外部目录中——而不是以任何方式在工作区或“在”Eclipse 中。
I then went to Windows / Preferences / Ant / Runtime / Classpath / Ant Home Entries and kill all the existing (default) values. I replaced them with the contents of the newly downloaded Ant lib directory.
然后我转到 Windows / Preferences / Ant / Runtime / Classpath / Ant Home Entries 并删除所有现有(默认)值。我用新下载的 Ant lib 目录的内容替换了它们。
I then added the downloaded junit.jar in Global Entries. It contains the org.hamcrest classes, just like the junit.jar included with Eclipse, so I don't know why this is necessary, but it is.
然后我在全局条目中添加了下载的 junit.jar。它包含 org.hamcrest 类,就像 Eclipse 中包含的 junit.jar 一样,所以我不知道为什么这是必要的,但确实如此。
That's it. My junit task in the build file works perfectly.
就是这样。我在构建文件中的 junit 任务运行良好。
回答by Bananeweizen
I had the same problem and tried many ways to alter the classpath. The easiest solution for me was to use the parameter fork="no" at the junit target. Seems like Eclipse can "automagically" do it better than me...
我遇到了同样的问题,并尝试了很多方法来改变类路径。对我来说最简单的解决方案是在 junit 目标上使用参数 fork="no"。似乎 Eclipse 可以“自动地”比我做得更好......
回答by Bananeweizen
I downloaded JUnit 4.7 and put junit-4.7.jar in my build path (instead of the older version). That solved it. I didn't touch ant.
我下载了 JUnit 4.7 并将 junit-4.7.jar 放在我的构建路径中(而不是旧版本)。那解决了它。我没碰蚂蚁。
回答by Rolf
I searched the Eclipse install directory from the top level for "hamcrest.jar" (wildcard '*' around hamcrest not showing in this web post). I then added the one found jar-file to the classpath in Ant build.xml. Fixed.
我在 Eclipse 安装目录的顶层搜索了“ hamcrest.jar”(hamcrest 周围的通配符 '*' 未显示在此网络帖子中)。然后我将找到的 jar 文件添加到 Ant build.xml 中的类路径。固定的。
回答by Jay Elston
I had this problem trying to run junit tests from Run -> Run As -> JUnit Test (I checked, and I do have the latest junit plugin (junit4_4.5.0) installed).
我在尝试从 Run -> Run As -> JUnit Test 运行 junit 测试时遇到了这个问题(我检查过,我确实安装了最新的 junit 插件(junit4_4.5.0))。
I created a new directory (externalJars) in the eclipse installation main directory, downloaded the most recent junit jar (4.8.1) there.
我在 eclipse 安装主目录中创建了一个新目录(externalJars),在那里下载了最新的 junit jar(4.8.1)。
Next, from Eclipse, with the project selected, I modified the jar files in the build path (Project --> Properties --> Java Build Path). When I first tried junit, I added the junit jar file from the plugin/org.junit4_4.5.0.v20090824 folder. I removed this jar file, and I added externalJars/junit-4.8.1.jar (the one I just downloaded) to the build path by clicking the "Add External Jars" button.
接下来,在 Eclipse 中,选择项目后,我修改了构建路径中的 jar 文件(项目 --> 属性 --> Java 构建路径)。当我第一次尝试 junit 时,我从 plugin/org.junit4_4.5.0.v20090824 文件夹中添加了 junit jar 文件。我删除了这个 jar 文件,并通过单击“添加外部罐子”按钮将 externalJars/junit-4.8.1.jar(我刚刚下载的那个)添加到构建路径中。
hamcrest.org is a library of "matchers". You can find info at:
hamcrest.org 是一个“匹配器”库。您可以在以下位置找到信息:
http://code.google.com/p/hamcrest/
http://code.google.com/p/hamcrest/
Provides a library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules.
提供一个匹配器对象(也称为约束或谓词)库,允许以声明方式定义“匹配”规则,以便在其他框架中使用。典型场景包括测试框架、模拟库和 UI 验证规则。
Hamcrest has been ported to Java, C++, Objective-C, Python and PhP.
Hamcrest 已被移植到 Java、C++、Objective-C、Python 和 PhP。
Note: Hamcrest it is not a testing library: it just happens that matchers are very useful for testing.
注意:Hamcrest 它不是一个测试库:碰巧匹配器对于测试非常有用。
回答by P?lOliver
I found the reason to an identical problem, but only when running the test with Ant on the commandline.
我找到了一个相同问题的原因,但只有在命令行上使用 Ant 运行测试时。
You might want to run Ant in debug mode (ant -d
) and watch out for this statement:
您可能希望以调试模式 ( ant -d
)运行 Ant并注意以下语句:
Im my case, this JAR was added to the end of my classpath when running the test and overrode my JUnit reference in my Ant script.
我的情况是,这个 JAR 在运行测试时被添加到我的类路径的末尾,并在我的 Ant 脚本中覆盖了我的 JUnit 引用。
It turned out that my Ant installation was bundled with a set of older JUnit libs. I had to remove the junit4.jar above in order to make my Ant classpath ref to take effect.
结果证明我的 Ant 安装与一组较旧的 JUnit 库捆绑在一起。我必须删除上面的 junit4.jar 以使我的 Ant 类路径引用生效。