java.lang.NoClassDefFoundError when trying to run JUnit tests from ANT
I'm having problems with a java.lang.NoClassDefFoundError when trying to run my JUnit tests from ANT using the runtestsreport task in thefollowing build.xml; I know the other tasks work, it's just the actual running of the tests that's the problem.
I've tried lots of adjustments based on questions posted on here but I can't fix it; what I really need is a dev to pair with but they've all gone home for the day.
Can anyone help?
The build.xml file is
<property name="build" location="C:\<path on my machine>\workspace\ESS\build"/>
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\tests"/>
<property name="frameworkbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\framework"/>
<property name="rawtestreports" location="C:\<path on my machine>\workspace\ESS\reports\raw"/>
<property name="htmltestreports" location="C:\<path on my machine>\workspace\ESS\reports\html"/>
<property name="reports" location="C:\<path on my machine>\workspace\ESS\reports"/>
<property name="src" location="C:\<path on my machine>\workspace\ESS\src"/>
<property name="testsrc" location="C:\<path on my m开发者_Go百科achine>\workspace\ESS\src\esstestapp\tests"/>
<property name="seleniumtools" location="C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2"/>
<property name="junittools" location="C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${rawtestreports}"/>
<mkdir dir="${htmltestreports}"/>
</target>
<path id="tools.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
</path>
<target name="compile" depends="init" description="compile the source" >
<javac srcdir="${src}" destdir="${build}" classpathref="tools.classpath" includeantruntime="false"/>
</target>
<target name="frameworkjar" depends="compile">
<jar destfile="${frameworkbuild}/jar/framework.jar" basedir="${frameworkbuild}" />
</target>
<target name="testsjar" depends="compile">
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
</target>
<path id="test.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
</path>
<target name="runtests" depends="compile, frameworkjar, testsjar" description="runs the tests" >
<junit printsummary="yes" haltonfailure="no" showoutput="yes" fork="no">
<classpath refid="test.classpath" />
<batchtest fork="yes" todir="${rawtestreports}">
<formatter type="xml"/>
<fileset dir="${testsrc}">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="runtestsreport" depends="runtests">
<junitreport todir="${reports}">
<fileset dir="${rawtestreports}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${htmltestreports}" />
</junitreport>
</target>
<target name="clean" description="Clean up">
<delete dir="${build}"/>
<delete dir="${reports}"/>
</target>
I get this error (and this type of error for each test class)
Credentials (wrong name: esstestapp/tests/Credentials)
java.lang.NoClassDefFoundError: Credentials (wrong name: esstestapp/tests/Credentials)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)...
I've ran ant in -verbose mode to check the classpath out but I can't see the problem (even though I know that it's staring me in the face)
[junit] Implicitly adding C:\Program Files\apache-ant-1.8.2\lib\ant-launcher
.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\apache-ant-1
.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-junit4.jar to C
LASSPATH
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] 'C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2\junit.jar;C:\User
s\waltersj\workspace\ESS\tools\junit-4.8.2\junitsrc.jar;C:\<path on my machine>\worksp
ace\ESS\tools\selenium-2.0rc2\apache-mime4j-0.6.jar;C:\<path on my machine>\workspace\
ESS\tools\selenium-2.0rc2\bsh-1.3.0.jar;C:\<path on my machine>\workspace\ESS\tools\se
lenium-2.0rc2\cglib-nodep-2.1_3.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-codec-1.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2
.0rc2\commons-collections-3.2.1.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-io-2.0.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.
0rc2\commons-lang-2.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
commons-logging-1.1.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
cssparser-0.9.5.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\guava-
r09.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\hamcrest-all-1.1.j
ar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\htmlunit-2.8.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\htmlunit-core-js-2.8.jar;C:\User
s\waltersj\workspace\ESS\tools\selenium-2.0rc2\httpclient-4.0.2.jar;C:\Users\wal
tersj\workspace\ESS\tools\selenium-2.0rc2\httpcore-4.0.1.jar;C:\<path on my machine>\w
orkspace\ESS\tools\selenium-2.0rc2\httpmime-4.0.1.jar;C:\<path on my machine>\workspac
e\ESS\tools\selenium-2.0rc2\jcommander-1.13.jar;C:\<path on my machine>\workspace\ESS\
tools\selenium-2.0rc2\jna.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0
rc2\json-20080701.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\juni
t-dep-4.8.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\nekohtml-1
.9.14.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\sac-1.3.jar;C:\U
sers\waltersj\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2-srcs.jar;
C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\serializer-2.7.1.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\testng-6.0.1-nobsh-noguice.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\xalan-2.7.1.jar;C:\Users\wa
ltersj\workspace\ESS\tools\selenium-2.0rc2\xercesImpl-2.9.1.jar;C:\Users\walters
j\workspace\ESS\tools\selenium-2.0rc2\xml-apis-1.3.04.jar;C:\<path on my machine>\work
space\ESS\build\esstestapp\tests\jar\tests.jar;C:\<path on my machine>\workspace\ESS\b
uild\esstestapp\framework\jar\framework.jar;C:\<path on my machine>\workspace\ESS;C:\P
rogram Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\apache-ant-1.8.2\lib\
ant-launcher.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\
apache-ant-1.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-jun
it4.jar'
Any eyes over this to help me get the tests running would be greatly appreciated.
Thanks
James
Update
I observe that if I edit tests.classpath to remove the following 2 lines then the error I see switches to a ClassNotFound which makes sense as it has no references to the .jar files that contain the classes.
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
With the above 2 lines present in the tests.classpath is happily finds them but with the wrong name and it's that error I don't know how to fix.
Problem caused by the following jar command:
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
The testsbuild property should be set to
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build"/>
Explanation
The file Credentials.class was compiled as the class esstestapp.tests.Credentials. It needs to be found on the classpath as the file:
esstestapp/tests/Credentials.class
精彩评论