Ant Junit task throws java.lang.ClassNotFoundException

I have a simple Junit test that keeps throwing a java.lang.ClassNotFoundException. Below is the folder structure

project:
|__build.xml
|__hamcrest-core-1.3.jar
|__junit.jar
|__src:
|  |__Simple.java
|__bin:
   |__Simple.class

Simple.java
import org.junit.Test;

public class Simple {
    @Test
    public void check() {
        assert (true);
    }
}

build.xml

<?xml version="1.0"?>
<project name="Ant-Test" default="main">
    <target name="main">
        <junit printsummary="yes" fork="yes" haltonfailure="no">
            <classpath path="hamcrest-core-1.3.jar" />
            <classpath path="junit.jar" />
            <formatter type="plain" />
            <batchtest fork="yes" todir="docs">
                <fileset dir="bin">
                    <include name="**.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

Error:
Testsuite: Simple
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Caused an ERROR
Simple
java.lang.ClassNotFoundException: Simple
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:292)

Please help me find the reason for why this error occurs. The script is able to find the test case but at the end an error is thrown.

Add the required resources to the test classpath:

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

that in your case will be:

<classpath>
   <pathelement path="bin" />
   <pathelement location="hamcrest-core-1.3.jar" />
   <pathelement location="junit.jar" />
</classpath>

As explained here

To make things a bit more flexible, we add one extra feature, which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, "" must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/ matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml.

'**' must be used to match folders, and not as wildcard to select partial file names. Change your ant with:

<fileset dir="bin">
    <include name="*.class" />
</fileset>

If you want to select all '*.class' files under every directory under bin/ then use:

<fileset dir="bin">
    <include name="**/*.class" />
</fileset>

You will have now an error cause by the test report writer. You need to create a docs folder and everything will work.

The final build.xml will be:

<?xml version="1.0"?>
<project name="Ant-Test" default="main">
    <target name="main">
        <mkdir dir="docs"/>
        <junit printsummary="yes" fork="yes" haltonfailure="no">
            <classpath location="hamcrest-core-1.3.jar" />
            <classpath location="junit-4.12.jar" />
            <classpath path="bin/" />
            <formatter type="plain" />
            <batchtest fork="yes" todir="docs">
                <fileset dir="bin">
                    <include name="**/*.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

Ant JUnit throws ClassNotFoundException, xml script. This time Ant is not able to locate my test class in the junit task. Is there something wrong with the way I am pointing to this class? I managed to fix it. Turned out that I had to specify class output directory and test class output directory in the classpath. Also hamcrest-core-1.3.jar wasn't there either.

Please try with following fileset tag:

<fileset dir="bin">                  
    <include name="**.java" />
</fileset>

another java.lang.ClassNotFoundException in ant's junit task , java.lang.ClassNotFoundException: UserActivityTest When I 'ant test' in command line to run my Junit test class, I continue getting ClassNotFoundException. [junit] at java.lang.Class. protected void tearDown​() throws Exception { Also http://ant.apache.org/manual/Tasks/java.html, it help me a lot! (9 replies) Hi! I'm trying to set up some automated builds for the first time here and I'm using a legacy ant build script that I've been hacking away at to get junit working. I am running ant 1.7, Junit 4.1 and java 1.5 on fedora core 5.

I'm not sure if junit task admits more than one classpath node, but anyway, it is a good practice to have just one. Moreover, your classpath is missing the classes directory. The right declaration would be like this:

<junit ...>
    <classpath>
        <pathelement path="bin"/>
        <pathelement location="hamcrest-core-1.3.jar"/>
        <pathelement location="junit.jar"/>
    </classpath>
    ...
</junit>

[junit] java.lang.ClassNotFoundException: UserActivityTest (Other , I verified that the ant-junit.jar file is in the lib directory as well. Task.perform(​Task.java:364) at org.apache.tools.ant. ClassNotFoundException: org/eclipse/​jdt/core/JDTCompilerAdapter at java.lang.Class. Answer #12 This is a FAQ; see http://ant.apache.org/faq.html#NoClassDefFoundError . When I 'ant test' in command line to run my Junit test class, I continue getting ClassNotFoundException. :banghead: Could anyone help on this? [junit] java.lang.ClassNotFoundException: UserActivityTest (Other Build Tools forum at Coderanch)

AntNewbies - ANT, I have an existing gradle build and Java project that was previously using Java 1.7 and Ant: Apache Ant™ version 1.9.3 compiled on December 23 2013 at java.lang.reflect. at org.gradle.api.internal.tasks.testing.junit. According to the ANT documentation for JUnit Task you have to do one of the following options to get junit tests to run as there is a dependency on an the junit.jar which is external to ANT: Note: You must have junit.jar available. You can do one of: Put both junit.jar and ant-junit.jar in ANT_HOME/lib.

Java.lang.NoClassDefFoundError when running test task, Running the cloverGenerateReport task on a simple Java project at the time of this writing) throws the following exception: > Task :test FAILED [cl CloverCompilerAdapter at org.apache.tools.ant.util. jcenter() } dependencies { testImplementation 'junit:junit:4.12' clover 'org.openclover:clover:4.3.1' }  Apache NetBeans Bugzilla – Bug 204729 ClassNotFoundException when running JUnit Test with "Compile on Save" on Last modified: 2011-11-29 15:58:29 UTC

ClassNotFoundException for CloverCompilerAdapter when using , ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java. Find file Copy import java.lang.reflect.Constructor; catch (final ClassNotFoundException e) {. throw new throw new BuildException("​Overdelegating loader", task. JUnit Description. This task runs tests from the JUnit testing framework. The latest version of the framework can be found at https://junit.org.This task has been tested with JUnit 3.0 up to JUnit 3.8.2; it won't work with versions prior to JUnit 3.0.

Comments
  • Thank you! This worked, had to add bin to classpath and use * for files...
  • classpath must contain "class" files, not java.