【发布时间】:2014-07-16 20:39:33
【问题描述】:
我不知道为什么我会从我的 ant build.xml 文件中得到这个异常。我检查了一下,一切都在类路径中。为什么要这么复杂?!
我过去在使用 Ant 时遇到过问题,似乎它总是与类路径有关。我使用两种方式指向 junit.jar:在 eclipse 中:window->preferences->ant->runtime->Ant Home->Add External Jars,以及在 build.xml 脚本中。这次 Ant 无法在 junit 任务中找到我的测试类。我指向这个类的方式有问题吗?
<target name="init">
<property name="sourceDir" value="src"/>
<property name="outputDir" value="build" />
<property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" />
</target>
<target name="clean" depends="init">
<delete dir="${outputDir}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${outputDir}" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>
<path id="classpath">
<pathelement location="${outputDir}" />
<pathelement location="${junitLocation}" />
</path>
<target name="testApplication" depends="compile">
<echo>Running the junit tests...</echo>
<junit fork="yes" haltonfailure="yes">
<test name="my.package.MyTest" />
<formatter type="plain" usefile="false" />
<classpath refid="classpath" />
</junit>
</target>
我总是得到:
[junit] Testsuite: my.package.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] my.package.MyTest
[junit] java.lang.ClassNotFoundException: my.package.MyTest
[junit] at java.net.URLClassLoader$1.run(Unknown Source)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Unknown Source)
BUILD FAILED
显然,Ant 找到了 junit.jar 并尝试开始测试,但为什么找不到我的测试类?我指向带有已编译测试类的文件夹。所以我知道 junit 至少在 Ant 的类路径上,但是 ClassNotFound 让我很困惑。
有什么想法吗?非常感谢!
【问题讨论】:
-
只是检查 - 你看到 ./build/my/package/MyTest.class 了吗?你确定它可以正常构建吗?
-
是的,我可以看到编译好的类,在正确的目录下,没有问题