TestNG ERROR Cannot find class in classpath

2019-01-14 15:37发布

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.

30条回答
你好瞎i
2楼-- · 2019-01-14 16:28

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

查看更多
不美不萌又怎样
3楼-- · 2019-01-14 16:29

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.

查看更多
虎瘦雄心在
4楼-- · 2019-01-14 16:31

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-14 16:31

I faced same problem but after close investigation.I found i was setting the class path wrong.

here what i was doing.

1-set class path=path for lib*; 2- java org.testng.TestNg testng.xml

Output-Cannot find class in classpath: tests.firstmodule

here firstmodule is my first class.

Solution

1-set class path=path for bin; lib*; 2- java org.testng.TestNg testng.xml

查看更多
混吃等死
6楼-- · 2019-01-14 16:32

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.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-14 16:32

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.

查看更多
登录 后发表回答