Error: Could not find or load main class testng.xml

9,759 views
Skip to first unread message

Charles Radley

unread,
Jun 30, 2016, 12:45:35 PM6/30/16
to testng-users

For the first time I am trying to run a testng test at the command line.

I am trying two different projects with two different testng.xml  files, and I am getting the same error for each.

Here is the first XML file which was auto generated by eclipse:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
  </test> <!-- Test -->
</suite> <!-- Suite -->

In the first project, the classes do not contain any testng annotations.

When I try to run it at command line from the project folder, I get this error:

C:\Users\charles\workspace\111 Learning Selenium Project - Demo>java -cp testng.jar testng.xml
Error: Could not find or load main class testng.xml

--------------------

And then here is the second project testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="TpPortalLogin">
    <parameter name="browser" value="chrome"/>
    <parameter name="url" value="http://xyz.local/"/>
    <parameter name="username" value="somebody"/>
    <parameter name="password" value="something"/>
        <classes>
            <class name="com.parameterization.TestParameters" />
        </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

the test suite contains four classes, all of which has @Test annotation, one of them also attempts to use @Parameters annotation.

Here is the transcript:

C:\Users\charles\Documents\test-automation\selenium\TP Portal\target\classes\tP_Portal>java -cp org.testng.TestNG testng.xml
Error: Could not find or load main class testng.xml

---

Any thoughts on why testng is not loading the testng.xml file in each case /

Thanks,

CFR.


⇜Krishnan Mahadevan⇝

unread,
Jul 1, 2016, 12:05:15 AM7/1/16
to testng...@googlegroups.com
Charles,

The correct syntax would be : java -cp <includeLocationsWhereYourJarsExist> org.testng.TestNG testng.xml

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Aditi Garg

unread,
Jul 16, 2020, 10:05:32 AM7/16/20
to testng-users
This is not working for me. please help
To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.

Krishnan Mahadevan

unread,
Jul 16, 2020, 11:13:44 AM7/16/20
to testng...@googlegroups.com
Aditi

Please share details on what you tried and what are you seeing as a problem.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Aditi Garg

unread,
Jul 16, 2020, 11:21:34 AM7/16/20
to testng-users
In IntelliJ I have testNG project, which I can successfully run via right-click on TestNG.xml and it will run all test classes mentioned in the XML file.
The thing I am now trying is --> to run the same testNG.xml File via command prompt

So when I am running the below commands I am getting an error :

E:\intellijws\automationandroid>java -cp E:\intellijws\automationandroid\lib\*; org.testng.TestNG testNG.xml
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

Also, noted that in my project there is no bin folder and lib folder. My project jars are all included in external libraries folder
Please let me know if you need any more details.

Thanks
Aditi

Aditi Garg

unread,
Jul 16, 2020, 11:44:05 AM7/16/20
to testng-users
I have created a lib folder, placed the testng jars into it and then run the command  -

E:\intellijws\automationandroid>java -cp E:\intellijws\automationandroid\lib\*; org.testng.TestNG testNG.xml


error is 

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

⇜Krishnan Mahadevan⇝

unread,
Jul 16, 2020, 12:41:07 PM7/16/20
to testng...@googlegroups.com
You are missing http://jcommander.org/#_download in your classpath.

You should consider using a build tool such as Maven/Gradle to setup your CLASSPATH, instead of you trying to download everything on your own and manage your CLASSPATH (which is prone to errors such as this)


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/b5b94dab-2a39-4474-af3a-04d12a39483fo%40googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 5:47:05 AM7/17/20
to testng-users
Hi Krishnan,

Thanks for your help, I have tried to resolve as you told :

Create lib and insert all jars to it. jcommander and testng both jars were there.
Run:  mvn dependency:build-classpath

Build success

Run java -cp E:\intellijws\automationandroid\lib\*; org.testng.TestNG testNG.xml


[TestNG] [ERROR]
Cannot find class in classpath: com.testcases.LoginTest
Exception in thread "main" java.lang.NullPointerException
        at org.testng.TestNG.getStatus(TestNG.java:211)
        at org.testng.TestNG.main(TestNG.java:1324)


though it is still running fine via intelli j but not via command line

To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.

Mallichetty Hemanth Kumar

unread,
Jul 17, 2020, 5:52:16 AM7/17/20
to testng...@googlegroups.com
in testNG.xml you may entered wrong class path, check once 

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/9308f34a-23b5-4d82-a5b1-6fe93fef82a0o%40googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 6:29:47 AM7/17/20
to testng-users
Attached is my xml file but this xml is working fine via intellij test execution, the process is working absolutely fine. 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="REGRESSION_TEST_SET" parallel="tests" thread-count="3" preserve-order="true">
    <listeners>
        <listener class-name="main.java.com.Report.EmailableReport"/>
     </listeners> 
    <test name="AUTOMATION">

        <classes>
            <class name="testcases.LoginTest"/>
          

        </classes>
    </test>
</suite>

Mallichetty Hemanth Kumar

unread,
Jul 17, 2020, 6:41:32 AM7/17/20
to testng...@googlegroups.com
keep the testNG file name as mentioned below. 

use "mvn test" in cmd


<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <!-- TestNG suite XML files -->
                        <suiteXmlFile>src/test/java/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
 </build>


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/57dd4530-4543-4043-8b50-a9bd2f3501f9o%40googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 7:11:30 AM7/17/20
to testng-users
all maven commands are already successfully running, my only concern is to run TestNG commands i.e to run testng.xml

For testng.xml - if I am trying to run testng.xml then I am getting the errors.

Thanks

Mallichetty Hemanth Kumar

unread,
Jul 17, 2020, 7:16:36 AM7/17/20
to testng...@googlegroups.com
To execute testNg.xml through command line, first u have to keep the path of testNg.xml in pom.xml file, so that u can run 

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/3eedfa59-37d3-4620-8c92-0c16d1320253o%40googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 7:18:41 AM7/17/20
to testng...@googlegroups.com
Yes, you're right.
I understand this and testng. xml path is already in pom. 

Mallichetty Hemanth Kumar

unread,
Jul 17, 2020, 7:20:17 AM7/17/20
to testng...@googlegroups.com
Can you share the command line screenshot

Aditi Garg

unread,
Jul 17, 2020, 7:24:52 AM7/17/20
to testng-users

img.PNG



On Friday, July 17, 2020 at 12:20:17 PM UTC+1, Mallichetty Hemanth Kumar wrote:
Can you share the command line screenshot

On Fri 17 Jul, 2020, 4:48 PM Aditi Garg, <gargad...@gmail.com> wrote:
Yes, you're right.
I understand this and testng. xml path is already in pom. 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 7:29:17 AM7/17/20
to testng-users
Via command line: if I run mvn test - the test executes successfully, but it will generate maven report not the testNG report.

Via intellij , testng.xml running and it generated the customized testNG.xml report. so this is I am trying to run via command line.

Hope that you understand my issue.

Thanks.

⇜Krishnan Mahadevan⇝

unread,
Jul 17, 2020, 10:49:52 AM7/17/20
to testng...@googlegroups.com
Aditi,
We seem to be going around in circles.

So lets take a step back.

Why do you need to be running tests via a "java -cp" command instead of leveraging the build tool (Gradle/Maven) to run your tests ?

Can you please clarify that ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/f676fc20-3dcb-443d-a14e-04460d7db338o%40googlegroups.com.

Aditi Garg

unread,
Jul 17, 2020, 10:59:22 AM7/17/20
to testng...@googlegroups.com
I simply want to execute my testng. xml file via command line to run the test class set mention in xml file so for that i am executing the command mentioned above java -cp..... 

Please correct me if I am wrong. 

(Earlier I was running mvn commands to execute test set but its not showing me the customized reports which i have implemented and added listners in testng. xml for customization)

Via google when i explored how to run testng. xml via command line it showed in all blogs to run java -cp <jar location>;<project location> org..... 
 

⇜Krishnan Mahadevan⇝

unread,
Jul 17, 2020, 12:46:30 PM7/17/20
to testng...@googlegroups.com
Aditi,

Few things.

If you are having a build tool that is managing your CLASSPATH (maven in your case), then you should be using "mvn clean test" to run your tests.
You will use "java -cp" mostly when you want to work with jars directly and source code is not available as class files.

Listeners would get invoked only if they are either added to your suite xml file or if you have added them as mandatory listeners via a service loader (https://rationaleemotions.com/testng_listeners/)


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Aditi Garg

unread,
Jul 17, 2020, 12:49:09 PM7/17/20
to testng...@googlegroups.com
Hmm, understood your points. 
Hopefully this helps me to clear and exceute this. 

Really Appreciate your help and time. 

Thanks
Aditi



Ashees Sahoo

unread,
Jul 18, 2020, 11:27:09 AM7/18/20
to testng...@googlegroups.com
Are u using Maven project?

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/9308f34a-23b5-4d82-a5b1-6fe93fef82a0o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages