[java] TestNG ERROR Cannot find class in classpath

I'm trying to run testng via command line and I have following things in classpath:

  • testng jar, jar of compiled test case file, and other required jars.
  • testng.xml points to appropriate class in the hierarchy.

Testng doesn't run instead it throws:

[TestNG] [ERROR]  Cannot find class in classpath: (name of testcase file)

I encountered the same issue before and I just added.

In classpath and it solved the problem. But this time it did not seem to solve it. Any workarounds? Thanks in advance.

This question is related to java classpath testng

The answer is


I was getting the same error - Cannot find class in classpath:

Solution: I had committed following mistakes:

  1. My class was not under any package. so i created a package and then created my class under that package.
  2. I had not mentioned full path in xml file. ex. . So i also corrected it.
  3. Then i right click on xml file and run as TestNG my program executed successfully.

If you have your project open in Eclipse(Mars) IDE: Right click on the project in eclipse Click on TestNG Click on "Convert to TestNG"

This creates a new testng.xml file and prompts you to overwrite the existing one (!!!Warning, this will erase your previous testng.xml!!!). Click Yes.

Run as Maven test build.


Was facing a similar issue using IntelliJ and in case it may be helpful to someone else what fixed it for me was to change the Use classpath of module field, to have it as the one that shows the package name.

enter image description here


add to pom:

        <build>
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <testSourceDirectory>${basedir}/test</testSourceDirectory>
             (...)
            <plugins>
                (...)
            </plugins>
        </build>

I just had this problem and was because the skip test option in the run configuration was checked. made that unchecked and build, then run the testNg, worked.


I just had this problem and I'm pretty confident that it's a bug in the TestNG Plugin for eclipse, since I was able to run my tests from the command line just fine.

The fix that seems to be working for me and others is just to clean your project (Project ? Clean). One person had to re-install the whole plugin.


This issue is coming due to some build error. 1. Clean Project - if issue not resolve 2. update maven project - If still not resolved 3. Go to build path from (right click on project- > properties -> java build path) now check library files. if you see an error then resolve it by adding missing file at given location.

I face the same issue, after lots of approaches I found 3rd solution to get that issue fixed


I have faced the similar issue when I tried to execute my Testng.xml file.

To fix you should move your class file from default pkg to some other pkg; ie create a new pkg and place your class file there


Eclipse ->Right Click on Project -> Build Path -> Configure -> Java Build Path.

Add Library -> JRE lib.

Add latest Java lib you are using in the system and remove other old JRE libs and save it.


To avoid the error

Cannot find class in classpath: (name of testcase file)

you need to hold Runner class and Test class in one place. I mean src and test folders.


right click on project -> go to maven -> update project

this fixed my problem when nothing else could


After changing the TestNG Output path(Project Properties-->TestNG-->Output Directory), its worked for me.


When I converted my project with default pacage and a class named addition, to testng.xml. It got converted as:

<classes>
      <class name=".addition"/>
</classes>

and was throwing error that class cannot be loaded. If I remove . before addition, the code works fine. So be careful with default package.


i was facing same issue solution is , you need to check testng file,your class created under package in this case class is not found through Test NG. perform below steps just convert your project in to Test NG and over write/rplace with new testNG file.

Again run this TestNG in this case it is working my case,


in Intellij, what resolved the issue for me was to open the Maven right side toolbar

choose "Clean" , "install" and hit the "Play" button:

see screenshot


Just problem with the class generating step. You can go to the project folder by explorer and delete classes. After that run "build" your project again. Now, your problem is fixed


Just do Eclipse> Project > Clean and then run the test cases again. It should work.

What it does in background is, that it will call mvn eclipse:clean in your project directory which will delete your .project and .classpath files and you can also do a mvn eclipse:eclipse - this regenerates your .project and .classpath files. Thus adding the desired class in the classpath.


make sure your suite.xml should not have .java extension

ex :

<test name="Test C1">
    <classes>
        <class name="com.taxi.suiteC.TestCase_C1" ></class>  
    </classes>
</test>

I ran into the same issue. Whenever I ran my test, it kept saying classes not found in the classpath. I tried to fix the testng.xml file, clean up the project and pretty much everything the answers say here, but none worked. Finally I checked my referenced libraries in build path and there were 34 invalid libraries, turns out I had renamed one of the folders from the path that I had taken the jar files from. I fixed the path and the test ran successfully.

So it might be fruitful to check if there are any errors in the referenced jar files before trying the other methods to fix this issue.


I had to make a jar file from my test classes and add it to my class path. Previously I added my test class file to class path and then I got above error. So I created a jar and added to classpath.

e.g

java -cp "my_jars/*" org.testng.TestNG testing.xml

If none of the above answers work, you can run the test in IDE, get the class path and use it in your command. Ex: If you are using Intellij IDEA, you can find it at the top of the console(screenshot below). Intellij Console

Clicking on the highlighted part expands and displays the complete class path.

you need to remove the references to jars inside the folder: JetBrains\IntelliJ IDEA Community Edition VERSION

java -cp "path_copied" org.testng.TestNG testng.xml

If the project is a Maven project, you can add maven surefire plugin and provide testng suite XML file path, navigate to the project directory and run the command: mvn clean install test

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>config/testrun_config.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

In my case, the package structure was not proper, it was like folders and sub-folders. I deleted that project and imported again with changing the respective jre. After that it worked for me. e.g. Folder structure- TestProject -TestSuite1 Package Structure- TestProject.TestSuite1


I got the same Problem while using maven

The solution that worked is, the name of the class in testNG file was different then the actual class Name

Since i changed the classname and testng file was not updated.

So make sure className in testNG file should match with the actual className

RestAssuredTest was mistaken written as RestAssuredest in testNG

One of the reason for this problem

<?xml version="1.0" encoding="UTF-8"?>

 <suite name="Suite">
   <test name="Test">
<classes>
  <class name="com.mycompany.app.AppTest"/>
  <class name="com.mycompany.app.RestAssuredTest"/>
 <class name="com.mycompany.app.appiumTests"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Before running the command for testng.xml file from command prompt, please check the following if you are missing them

  1. In the command prompt, make sure you are navigating to the folder where you have placed the testng.xml file.
  2. after navigating to that, set CLASSPATH and include the testng jar file location, selenium-server jar file location(if you are working with selenium webdriver), bin folder location of your project which contains all the .class files of your project.
    e.g., set CLASSPATH=C:\Selenium\testng-5.8-jdk15.jar;C:\Selenium\selenium-server-standalone-2.31.0.jar;C:\SeleniumTests\YourProject\bin
  3. Now run the command java org.testng.TestNG testng.xml.

I was into the same situation but the above things worked for me.


If you have renamed your testNG class and in the test suite xml file, its not renamed, you will get this error. I corrected in xml file also and it worked for me.


If your package name is FabFurnishPackage and your class name is registration then the code in xml should be

<classes>
<class name="FabFurnishPackage.registration">
</classes>

[TestNG] [ERROR] Cannot find class in classpath: registration issue is fixed...


Please make sure that you have specified your class/package in TestNG.xml file.

<test name="test1" preserve-order="false">
    <classes>
        <class name="ABC"/>
    </classes>
</test>


I was facing the same issue and what I tried is as below.

  1. Clean the project (Right click on pom.xml and clean)
  2. And update the maven project (Project > Maven > Update Maven Project)

This solved the issue.


I was also facing the same issue. I have tried to set the class path in different way and it works for me:

set classpath="projectpath"\bin;"projectpath"\lib\*     

press Enter

java org.testng.TestNG testng.xml   

press Enter again

I hope this works!


I tried all suggestions above, and it did not work for my case. It turns out that my project build path has an error (one of the dependent jar cannot be found). After fixing that error, testng works again in my Eclipse.


Check the class hierarchy. If the package is missing in your class you'll get this error. So, try to create a new testing class by selecting the package.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to classpath

spark submit add multiple jars in classpath How to resolve this JNI error when trying to run LWJGL "Hello World"? Intellij Cannot resolve symbol on import Eclipse error "Could not find or load main class" "Could not find or load main class" Error while running java program using cmd prompt Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger getting JRE system library unbound error in build path Run a JAR file from the command line and specify classpath How do I resolve ClassNotFoundException? File inside jar is not visible for spring

Examples related to testng

element not interactable exception in selenium web automation how to use List<WebElement> webdriver Error: org.testng.TestNGException: Cannot find class in classpath: EmpClass How to run TestNG from command line How do I setup the InternetExplorerDriver so it works TestNG ERROR Cannot find class in classpath Order of execution of tests in TestNG