java Ant JUnit ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12745465/
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 ClassNotFoundException
提问by IAmYourFaja
I realize there are a lot of similar questions as this one, however after reading over most of them I seem to be having the same problem, but for different reasons.
我意识到有很多与这个类似的问题,但是在阅读了其中的大部分之后,我似乎遇到了同样的问题,但原因不同。
I'm running a <junit>
task inside my Ant build and am getting a ClassNotFoundException
. In most of the similarly-related questions (such as the one above), this turned out to be various degrees of the author simply not configuring the JUnit classpath correctly. Although it may very well be the same for me, none of the suggestions to any of these other questions have worked for me.
我正在<junit>
我的 Ant 构建中运行一个任务,并得到一个ClassNotFoundException
. 在大多数类似相关的问题中(例如上面的问题),结果证明是作者在不同程度上没有正确配置 JUnit 类路径。虽然对我来说很可能是一样的,但对这些其他问题的任何建议都没有对我有用。
My project directory structure:
我的项目目录结构:
MyProject/
src/main/java/
FakeObject.java
...the rest of my Java main source
src/test/java
FakeObjectUnitTest.java
...the rest of my Java test source
bin/main
FakeObject.class
...the rest of main Java binaries
bin/test
FakeObjectUnitTest.class
...the rest of main Java binaries
lib/main
...my main dependencies
lib/test
junit-4.10.jar
hamcrest-1.1.jar
...the rest of my test dependencies
gen/reports
...where JUnit should be placing all test reports
build/
build.xml
build.properties
In src/test/java/FakeObjectUnitTest.java
:
在src/test/java/FakeObjectUnitTest.java
:
// Package and imports omitted
public class FakeObjectUnitTest {
@Test
public void doSomething() {
int x = 5;
Assert.assertEquals(x, 5);
}
}
My build.xml
:
我的build.xml
:
<project name="" basedir="..">
<!-- Main classpath. -->
<path id="main.class.path">
<fileset dir="bin/main"/>
</path>
<!-- Test classpath. -->
<path id="test.class.path">
<fileset dir="bin/test"/>
</path>
<!-- JUnit classpath. -->
<path id="junit.class.path">
<fileset dir="lib/main" includes="*.jar"/>
<fileset dir="lib/test" includes="*.jar"/>
<path refid="main.class.path"/>
<path refid="test.class.path"/>
</path>
<!--
All previous targets omitted for brevity (such as for compiling, etc.), but
I assure you all that they work and compile FakeObject.java/FakeObjectUnitTest.java into
FakeObject.class/FakeObjectUnitTest.class respectively, and place them in the correct
bin directories.
-->
<target name="run-junit" depends="compile">
<property name="junit.path" refid="junit.class.path"/>
<echo message="JUnit ClassPath is: ${junit.path}"/>
<junit printsummary="yes" haltonerror="yes" haltonfailure="yes">
<classpath refid="junit.class.path"/>
<formatter type="xml"/>
<batchtest todir="gen/reports">
<fileset dir="src/test/java">
<include name="**/*UnitTest*.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
When I run this target from the command-line:
当我从命令行运行这个目标时:
run-junit:
[echo] Conducting unit tests with JUnit.
[echo] JUnit ClassPath is: /<path-to-my-project>/MyProject/lib/test/hamcrest-1.1.jar:/<path-to-my-project>/MyProject/lib/test/junit-4.10.jar:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:/<path-to-my-project>/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class
[junit] Running com.myproject.client.FakeObjectUnitTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
I do this to provethat it sees FakeObjectUnitTest.class
on the JUnit classpath. However, it fails with an error. When I go into the XML TEST report for this test, I see the following:
我这样做是为了证明它可以FakeObjectUnitTest.class
在 JUnit 类路径上看到。但是,它失败并出现错误。当我进入这个测试的 XML TEST 报告时,我看到以下内容:
<error message="com.myproject.client.FakeObjectUnitTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: com.myproject.client.FakeObjectUnitTest
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
</error>
When I run Ant in verbose mode (-v
) the only hintI see are a bunch of warnings along the lines of:
当我以详细模式 ( -v
)运行 Ant 时,我看到的唯一提示是一堆警告:
Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource x
忽略异常 java.util.zip.ZipException:打开 zip 文件读取资源 x 时出错
...where x
is the name of a class file.
...x
类文件的名称在哪里。
I found thisquestion on SO, and the problem turned out to be that the author had added class files to the classpath, not JARs (which makes JUnit unhappy). So I tried removing main.class.path
and test.class.path
from junit.class/path
and then I get an exception that none of the class files can be found.
我在 SO 上发现了这个问题,结果发现问题是作者在类路径中添加了类文件,而不是 JAR(这让 JUnit 不高兴)。所以我尝试删除main.class.path
and test.class.path
fromjunit.class/path
然后我得到一个异常,即找不到任何类文件。
Any ideas or thoughts as to what is going on? Thanks in advance, and sorry for the -v
question here, but I figured the more details, the better.
关于发生了什么的任何想法或想法?提前致谢,-v
对这里的问题表示抱歉,但我认为细节越多越好。
采纳答案by oers
You need to use for package-like structures:
From the manual:
您需要使用类似包的结构:
从手册中:
Wherever path-like values need to be specified, a nested element can be used. This takes the general form of:
<classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath>
只要需要指定类似路径的值,就可以使用嵌套元素。这采用以下一般形式:
<classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath>
So your solution would look like:
所以你的解决方案看起来像:
<!-- JUnit classpath. -->
<path id="junit.class.path">
<fileset dir="lib/main" includes="*.jar"/>
<fileset dir="lib/test" includes="*.jar"/>
<pathelement location="bin/main"/>
<pathelement location="bin/test"/>
</path>
回答by Jesper
Looking at your classpath:
查看您的类路径:
...:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:...
You can't put an individual class in the classpath like that. Instead, put the directory that's at the base of your package hierarchy in the classpath:
您不能像那样将单个类放在类路径中。相反,将位于包层次结构基础的目录放在类路径中:
...:/<path-to-my-project>/MyProject/bin/main:...
回答by chetan madnaik
make modification in run-junit target as follows,
在 run-junit 目标中进行如下修改,
<batchtest todir="gen/reports">
<fileset dir="bin/tests">
<include name="**/*UnitTest*.class"/>
</fileset>
</batchtest>