我无法弄清楚为什么我从我的ant build.xml文件中得到这个异常。我检查了一切,都在类路径中。为什么要这么复杂?!ant的junit任务中的另一个java.lang.ClassNotFoundException
我在过去遇到过Ant,看起来它总是与类路径相关。我使用两种方式指向junit.jar:在eclipse中:window-> preferences-> ant-> runtime-> Ant Home->添加外部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
显然,蚂蚁发现的junit.jar并尝试开始测试,但为什么不能找到我的测试类?我指向编译测试类的文件夹。所以我知道junit至少在Ant的classpath上,但ClassNotFound让我困惑。
也许有任何想法?非常感谢!
只需检查 - 你会看到./build/my/package/MyTest.class?你确定它的构建正确吗? – duffymo 2009-08-01 03:19:23
是的,我可以看到编译的类,在正确的目录中,没有问题 – denchr 2009-08-01 03:30:15