bryan63
bryan63

Reputation: 13

Java Ant Error : ClassNotFoundException

I'm a beginner with Ant but I have a problem when I try to use JUnit :

<target name="test" depends="compile" description="JUnit Tests">
    <junit printsummary="yes" failureproperty="junit.failure" fork="yes">
        <classpath refid="junit.classpath"/>
        <classpath path="${myAntTestBin.dir}"/>
        <formatter type="plain" usefile="false"/>
        <test name="fr.isima.myAntTest.ProjectTest"/>
    </junit>
</target>

But now, I have these errors :

test:
    [junit] Running fr.isima.myAntTest.ProjectTest
    [junit] Testsuite: fr.isima.myAntTest.ProjectTest
    [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] fr.isima.myAntTest.ProjectTest
    [junit] java.lang.ClassNotFoundException: fr.isima.myAntTest.ProjectTest
    [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:186)
    [junit]
    [junit] Test fr.isima.myAntTest.ProjectTest FAILED

What is the problem ? Thank you in advance !

Upvotes: 1

Views: 188

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

I suspect this is the problem:

<test name="${myAntTestBin}/ProjectTest.class"/>

It's looking for the name of the class, not the location:

<test name="ProjectTest"/>

... with a package name qualifying it if appropriate (which you really should have, of course). So for example:

<test name="foo.bar.ProjectTest" />

And also, as JB Nizet mentions, this:

<classpath path="${myAntTestBin}"/>

should be:

<classpath path="${myAntTestBin.dir}"/>

to match your declaration:

<property name="myAntTestBin.dir" value="../myAntTest/bin/fr/isima/myAntTest"/>

EDIT: Now that you've edited the question (completely) it looks like you've got the wrong classpath, and it should actually be:

<property name="myAntTestBin.dir" value="../myAntTest/bin"/>

The classpath should be the base of directory structure containing your classes. This is just like in normal Java when you'd run:

java -cp bin foo.bar.Baz

if you had a directory structure of bin/foo/bar containing Baz.class.

Upvotes: 1

Related Questions