Below is my Build.xml file. I am trying to execute my TestNG.xml through Build.xml but I am getting the below error every time:

[testng] Cannot find class in classpath:

The below file is working fine till the compile target. I guess there are some issues with the code written in exec target.

I have tried resolving the issue by a number of methods that I found on stackoverflow such as: Cleaning the project, updating the TestNG library but it didn't work out.

Can anybody help me on this?

<property name="lib" value="./lib" />

<property name="bin" value="./bin" />

<path id="Sample Ant build.classpath">
    <pathelement location="${output}" />
    <fileset dir="${lib}">
        <include name="**/*.jar" />

<target name="init">
    <delete dir="${bin}">
    <mkdir dir="${bin}" />

<target name="compile" depends="init">
    <javac srcdir="${src}" 
        <classpath refid="Sample Ant build.classpath"/>

<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="Sample Ant build.classpath" />

<target name="exec" depends="compile">
    <mkdir dir="${testng_report_dir}" />
    <testng outputdir="${testng_report_dir}" classpathref="Sample Ant build.classpath" useDefaultListeners="true">
        <xmlfileset dir="${basedir}" includes="Testng.xml" />

  • if you are using pom.xml for dependency management, then change the scope to compile in testng dependency. Apr 24, 2017 at 8:53
  • No. I am using build.xml. Thank you for your response. I just resolved this issue. Thank you. Apr 24, 2017 at 14:35

1 Answer 1


I just resolved this issue. The problem was in the classpath that I had set in the build.xml:

<path id="classpath">
    <pathelement location="${bin}" />
    <fileset dir="${lib}">
        <include name="**/*.jar" />
  • bin directory contains the class file which gets generate upon compilation of your .java file Apr 24, 2017 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.