Monday, October 24

Running Selenium TestNG test from command line

When I tried to run TestNG command from command line, I get following error. This is mainly because java cannot find class path and jar file of TestNG
C:\Selenium\Selenium_tests\DemoProject java org.testng.TestNg
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNg
Caused by: java.lang.ClassNotFoundException: org.testng.TestNg
        at$ Source)
        at Method)
        at Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.testng.TestNg.  Program will exit.
So below are the steps to get it working

1. Navigate to directory of your project (if you are using Eclipse IDE).Create and set a path variable named “ProjectPath” by entering following command. Do echo command to check what is the value that we just set is right

C:\Selenium\Selenium_tests\DemoProject set ProjectPath=C:\Selenium\Seleni
C:\Selenium\Selenium_tests\DemoProject echo %ProjectPath%
2.  Now we after setting this project path, we need to create classpath that points to selenium/Testng class files and jar files along with testng jar file. My project setup is  like below and I keep jar files in folder called lib and eclipse creates complied classes in directory called bin automatically when you execute your test cases from eclipse
Below is command to set the classpath for jars in lib folder and binary classes/folders in bin folder and do a echo to check the path that is been set

C:\Selenium\Selenium_tests\DemoProject set classpath=%ProjectPath%
C:\Selenium\Selenium_tests\DemoProject echo %classpath%
3. Now execute the java command for TestNG as shown below

C:\Selenium\Selenium_tests\DemoProject java org.testng.TestNG 

[TestNG] Running:
Total tests run: 6, Failures: 0, Skips: 6
Configuration Failures: 4, Skips: 12


  1. Thanks Smita for visiting the blog. Glad to hear it helped to solve the problem

  2. Hi,
    This was really useful. Thank you :)


  3. hi i tried with all ur above steps but when executed getting the below error
    Error: Could not find or load main class org.testng.TestNG

    please help

  4. Hi...........
    This is not working on my side ...after executing steps 3 It is showing an error "java.lang.NoClassDefFoundError"

    In My eclipse project "lib" folder was not there so i created it on my own including the
    2) testng.jar
    3) testng-sources.jar
    4) junit-4.8.2.jar
    5) guava-base-r03.jar
    6) guava-collections-r03.jar

    Is there something missing ? please help !

  5. thankyou so much for the solution

  6. Thank you, thank you, this works, 20 April 2020 was my lucky day

  7. showing Error: Could not find or load main class org.testng.TestNG, please help



Related Posts Plugin for WordPress, Blogger...