Hello fellow ant users. I have a problem with ant and junit that I am
pretty sure is a class loader problem. When I run the junit task on my
test cases I always get a ClassNotFoundException on my test case classes.
I am sure they are on the classpath so that makes me think it is a class
loader problem.
Here is what the error looks like when I run my ant build
Buildfile: build.xml
Trying to override old definition of datatype junit
init:
compile:
unit:
[junit] Running com.mycom.MyClassTestCase
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Testsuite: com.mycom.MyClassTestCase
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Null Test: Caused an ERROR
[junit] com.mycom.MyClassTestCase
[junit] java.lang.ClassNotFoundException: com.mycom.MyClassTestCase
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
[junit] at java.security.AccessController.doPrivileged(Native
Method)
[junit] at
java.net.URLClassLoader.findClass(URLClassLoader.java:187)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
[junit] at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
[junit] at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:141)
I get this error when I run the unit task from my build.xml file. Here is
my build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="dist" basedir=".">
<property name="source_dir" value="."/>
<property name="build_dir" value="${basedir}/build"/>
<property name="distribute_dir" value="dist"/>
<!-- these will be set by the calling builder. They are defined
here so we can run in standalone mode -->
<property name="javac.debug" value="true" />
<property name="javac.debuglevel" value="source" />
<property name="javac.exe" value="${java.home}/../bin/javac" />
<property name="javac.deprecation" value="off"/>
<property name="javac.verbose" value="no"/>
<property name="javac.optimize" value="off"/>
<property name="javac.nowarn" value="off"/>
<taskdef name="junit"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<fileset
file="${basedir}/../ThirdPartyJavaLibs/ant-junit.jar"/> <!-- I want this
to be <classpath refid="project.classpath" /> but I am leaving like this
to demonstrate where I moved the junit jars to-->
<fileset
file="${basedir}/../ThirdPartyJavaLibs/junit.jar"/>
</classpath>
</taskdef>
<path id="project.classpath">
<fileset dir="${basedir}/../ThirdPartyJavaLibs">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${build_dir}">
<include name="**/*.class"/>
</fileset>
</path>
<target name="init">
<tstamp/>
<mkdir dir="${build_dir}" />
<mkdir dir="${distribute_dir}/lib"/>
</target>
<target name="compile" depends="init">
<javac deprecation="${javac.deprecation}"
verbose="${javac.verbose}"
optimize="${java.optimize}"
nowarn="${javac.nowarn}"
srcdir="${source_dir}"
destdir="${build_dir}"
executable="${javac.exe}"
fork="true"
debug="${javac.debug}"
debuglevel="${javac.debuglevel}">
<classpath
refid="project.classpath"/>
</javac>
</target>
<target name="dist" depends="compile">
<jar
jarfile="${distribute_dir}/lib/${ant.project.name}.jar">
<fileset dir="${build_dir}">
<include name="**/*.class"/>
<exclude name="**/*TestCase.class"/>
</fileset>
</jar>
</target>
<target name="clean" depends="init">
<delete dir="${distribute_dir}"/>
<delete dir="${build_dir}"/>
</target>
<target name="unit" depends="compile">
<junit printsummary="yes" fork="yes">
<classpath
refid="project.classpath"/>
<formatter type="brief" usefile="false"/>
<batchtest todir="${distribute_dir}">
<fileset dir="${build_dir}">
<include
name="**/*TestCase.class"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
After reading the ant FAQ about classloaders it sounds like I needed to
pull the ant-junit.jar and junit.jar classes out of the $ANT_HOME/lib dir.
That is why I added the taskdef for the junit task in the buildfile. That
did not work for me though.
So I get the above error regardless if ant-junit.jar and junit.jar are in
the $ANT_HOME/lib dir or not.
Also I did not start getting the warning about the junit task until I set
up a junit taskdef when I moved the jars out of $ANT_HOME/lib. I'm not
sure if this warning is a problem or not.
Another interesting error I noticed(I'm not sure if it is related to the
same problem) is that if I change fork="yes" to fork="no" in the junit
task attributes I get the following error
Buildfile: build.xml
Trying to override old definition of datatype junit
init:
compile:
unit:
[junit] Running
com.ibm.md.tse.dmiw.security.logic.SecurityControlTableLogicImplTestCase
BUILD FAILED
C:\build.xml:71:
org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter is
not a JUnitResultFormatter
So I am hoping someone out there can help me. I'm not sure what I can do.
Performing a Google search has produced many forum posts where this
problem is discussed but I have yet to find one where someone produces a
solution.
I forgot to mention I am running a 1.4.2 java vm
Dave Noel
[EMAIL PROTECTED]