ant+junit组合出现ClassNotFoundException错误

一个简单的junit测试,在eclipse中,测试用例run as junit.正确,绿色的进度条。
后来结合自己最近刚学的,通过ant+junit来实现编译和测试。build.xml文件内容如下:
[code="java"]<?xml version="1.0" encoding="UTF-8"?>



<path id="project.classpath">       
    <fileset dir="C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib">
        <include name="**/*.jar"/>
    </fileset>
    <filelist id="file" dir="D:\My Documents\book spec helpdoc\help doc\junit4.5">
        <file name="junit-4.5.jar"/>
        <file name="junit-dep-4.5.jar"/>
    </filelist>
    <filelist id="file2" dir="D:\My Documents\book spec helpdoc\help doc\dom4j-1.6.1">
        <file name="dom4j-1.6.1.jar"/>          
    </filelist> 
    <filelist id="file3" dir="D:\My Documents\book spec helpdoc\help doc\apache-log4j-1.2.15">
        <file name="log4j-1.2.15.jar"/>         
    </filelist> 
    <pathelement location="D:\My Documents\book spec helpdoc\help doc\commons-lang-2.3\commons-lang-2.3.jar"/>  
</path>
<target name="junitTest" depends="compileAll">
    <junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">         
        <formatter type="plain" usefile="false"/>
        <test name="test.examsystem.common.AnotherTestPage"/>       
    </junit>
</target>
<target name="compileAll">
    <javac srcdir="${common.dir}" destdir="${dest.dir}">
        <classpath refid="project.classpath"/>
    </javac>
    <javac srcdir="${src.dir}" destdir="${dest.dir}">
        <classpath refid="project.classpath"/>              
    </javac>        
</target>

[/code]
程序导入的jar,是因为在整个的copileAll过程中,有引用到。
通过命令窗口来运行时,却出现java.lang.ClassNotFoundException: test.examsystem.common.AnotherTestPage这个错误。
有查到javaeye中有个兄弟也提出过这个问题(http://www.iteye.com/problems/10228),但是是无满意答案回复。而且他自己解决的办法不适用我这个。所以,找大家帮个忙。

[b]问题补充:[/b]
应当没有这个问题,因为在compileAll任务中,是没有错误的。
自己通过增减构建文件内容,将 这句删掉是没有问题的。
刚开始的时候,我也是怀疑没有这个文件。将原来已经有的class文件全部删除后再运行一次,所有的class文件是都会出来的。

将class文件拖到eclipse中打开时,类的定义有:
public class test.examsystem.common.AnotherTestPage extends junit.framework.TestCase{...}.应当是可以排队找不到类的问题。
问题可能在于类名那里,test.examsystem.common.AnotherTestPage .(jdk 1.5)

[b]问题补充:[/b]
通过ant -debug,得到下面的结果。
[code="java"]
Apache Ant version ${project.version} compiled on ${TODAY}
Buildfile: build.xml
Adding reference: ant.PropertyHelper
Detected Java version: 1.5 in: C:\Program Files\Java\jdk1.5.0_16\jre
Detected OS: Windows XP
Adding reference: ant.ComponentHelper
Setting ro project property: ant.file -> D:\workspace\examsystem\src\ant\build.x
ml
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile D:\workspace\examsystem\src\ant\build.xml with URI = file:/D:/
workspace/examsystem/src/ant/build.xml
Setting ro project property: ant.project.name -> build
Adding reference: build
Setting ro project property: ant.file.build -> D:\workspace\examsystem\src\ant\b
uild.xml
Project base dir set to: D:\workspace\examsystem\src\ant
+Target:
+Target: junitTest
+Target: compileAll
[antlib:org.apache.tools.ant] Could not load definitions from resource org/apach
e/tools/ant/antlib.xml. It could not be found.
Setting project property: src.dir -> D:\workspace\examsystem\src
Setting project property: common.dir -> D:\workspace\mycommon
Setting project property: dest.dir -> D:\workspace\examsystem\WebContent\WEB-INF
\classes
Adding reference: project.classpath
Adding reference: file
Adding reference: file2
Adding reference: file3
Attempting to create object of type org.apache.tools.ant.helper.DefaultExecutor
Adding reference: ant.executor
Build sequence for target(s) `junitTest' is [junitTest]
Complete build sequence is [junitTest, compileAll, ]

junitTest:
[junit] Found D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\li
b\junit-4.5.jar
[junit] Found D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\li
b\ant-launcher.jar
[junit] Found D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\li
b\ant.jar
[junit] Found D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\li
b\ant-junit.jar
[junit] Implicitly adding D:\My Documents\book spec helpdoc\help doc\apache-
ant-1.7.1\lib\junit-4.5.jar;D:\My Documents\book spec helpdoc\help doc\apache-an
t-1.7.1\lib\ant-launcher.jar;D:\My Documents\book spec helpdoc\help doc\apache-a
nt-1.7.1\lib\ant.jar;D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1
\lib\ant-junit.jar to CLASSPATH
[junit] Executing 'C:\Program Files\Java\jdk1.5.0_16\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] 'D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\jun
it-4.5.jar;D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant-l
auncher.jar;D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant.
jar;D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant-junit.ja
r'
[junit] 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
[junit] 'test.examsystem.common.AnotherTestPage'
[junit] 'filtertrace=true'
[junit] 'haltOnError=true'
[junit] 'haltOnFailure=true'
[junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnit
ResultFormatter'
[junit] 'showoutput=false'
[junit] 'outputtoformatters=true'
[junit] 'logtestlistenerevents=true'
[junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitRe
sultFormatter'
[junit] 'crashfile=D:\workspace\examsystem\src\ant\junitvmwatcher4124.proper
ties'
[junit] 'propsfile=D:\workspace\examsystem\src\ant\junit4125.properties'
[junit]
[junit] The ' characters around the executable and arguments are
[junit] not part of the command.
Execute:Java13CommandLauncher: Executing 'C:\Program Files\Java\jdk1.5.0_16\jre\
bin\java.exe' with arguments:
'-classpath'
'D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\junit-4.5.jar;D
:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant-launcher.jar;
D:\My Documents\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant.jar;D:\My Do
cuments\book spec helpdoc\help doc\apache-ant-1.7.1\lib\ant-junit.jar'
'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
'test.examsystem.common.AnotherTestPage'
'filtertrace=true'
'haltOnError=true'
'haltOnFailure=true'
'formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormat
ter'
'showoutput=false'
'outputtoformatters=true'
'logtestlistenerevents=true'
'formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatte
r'
'crashfile=D:\workspace\examsystem\src\ant\junitvmwatcher4124.properties'
'propsfile=D:\workspace\examsystem\src\ant\junit4125.properties'

The ' characters around the executable and arguments are
not part of the command.
[junit] Running test.examsystem.common.AnotherTestPage
[junit] Testsuite: test.examsystem.common.AnotherTestPage
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] test.examsystem.common.AnotherTestPage
[junit] java.lang.ClassNotFoundException: test.examsystem.common.AnotherTest
Page
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:164)
[junit]

BUILD FAILED
D:\workspace\examsystem\src\ant\build.xml:24: Test test.examsystem.common.Anothe
rTestPage failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResul
t(JUnitTask.java:1840)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitT
ask.java:837)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue
(JUnitTask.java:1785)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitT
ask.java:785)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
at org.apache.tools.ant.Main.runBuild(Main.java:760)
at org.apache.tools.ant.Main.startAnt(Main.java:219)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

Total time: 3 seconds
[/code]
目前,我自己正在试着去修改报错误的类。打印提示信息,暂时没有进展。
[b]问题补充:[/b]
多谢grandboy的解答,正是你说的这个问题导致我的ant+junit出错。
悠修改你的一个笔误:
[code="java"]
要改成

[/code]
运行正常,看到了这个结果:
[code="java"]
Buildfile: build.xml

compileAll:

junitTest:
[junit] Running test.examsystem.common.AnotherTestPage
[junit] Testsuite: test.examsystem.common.AnotherTestPage
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.031 sec
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.031 sec
[junit]
[junit] Testcase: testPage took 0.015 sec
[junit] Testcase: testGetPageNavigateInt took 0 sec
[junit] Testcase: testGetPageNavigate took 0 sec

BUILD SUCCESSFUL
Total time: 3 seconds
[/code]
再次多谢!!!!!!!

2个回答















这样子应该就行了。你都没有告诉junit到哪里去测试类,它当然找不到那个类了。

你看下是不是没有产生这个.class文件

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐