What is TestNG ERROR – Cannot find Class in Classpath?

User may encounter Cannot find class in classpath exception while executing tests in TestNG framework. The meaning of this error is TestNG unable to find the class as mentioned in testng.xml. This can be caused because of the following reasons βˆ’

  • In the TestNG XML, the class tag having the name attribute. User should define the class name as <package_name>.<class_name> format. It should not have the .java extension.

  • In the TestNG XML, the class file is incorrect and hence unable to determine the classpath of the class.

  • Errors within the project are present and may require a clean project.

  • In the TestNG XML, the class file name is incorrect.

  • Sometimes, user doesn’t have any exception or wrong name, yet gets the classpath not found exception.

In this article, we will see what checklists and way are to resolve the error.

Approach/Algorithm to Solve this Problem

  • Step 1: Make sure class name is provided in proper format and name is correctly typed in testng.xml. If class is inside the package write the class name as <package>.

  • Step 2: Make sure class name is not ending with .java extension in testng.xml.

  • Step 3: If above 2 checks are reviewed and issue yet persists, clean the project by navigating as Project βˆ’> Clean (in eclipse) while file βˆ’>Invalidate cache/restart (in Intellij) or you can do Build βˆ’> Rebuild project

  • Step 4: If issue still persists and project is using maven, try to do update maven project.

  • Step 4: If none of above solution works, Navigate to build path as right click on projectβˆ’ > properties βˆ’> java build path. Now check library files. if you see any error then resolve it by adding missing file at given location.

Updated on: 21-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started