Java class file failed to load testng class when run through command line

I have this piece of code, which invokes testNG with an XML file from the main method. I am trying to invoke this class file from command line via the following:

java -cp My_Automation.jar com.mycomp.test.sanity.InvokeTestNGTest

However, this fails with the following message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener

I have tried running this through Eclipse, which works perfectly fine, but it fails when invoked from the command line interface. All testing JARs are placed in the classpath. I don't understand the discrepancy.

Here is my code:

package com.mycomp.test.sanity;

import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.TestListenerAdapter;


public class InvokeTestNGTest {
    public static void main(String[] args) {


        List<String> xmlFileList = new ArrayList();
        xmlFileList.add("ILIO_testNG.xml");
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();

        testng.setTestSuites(xmlFileList);
        testng.addListener(tla);
        testng.run();

        }

}

Answers


Unless all your jar files are in the same directory, you'll need to specify the path to each jar in the classpath. You'll also need to make sure you're not missing any other JAR files.

java -cp "/path/to/project/My_automation.jar; /path/to/project/libs/testng-5.5.jar" ... 

Please provide the testng.jar in your classpath while running from command prompt.

java -cp "My_Automation.jar;testng-5.5.jar" com.mycomp.test.sanity.InvokeTestNGTest

Thanks


Which ever jar contains the org.testng.ITestListener class is not on your runtime classpath. Sergii Zagriichuk mentioned a testng.jar?


Realized that I had exported it as just a jar instead of executable jar. This was not picking up the dependency jar's for the project. Thanks for the pointers


Need Your Help

Using Listview and Sqlite

android sqlite listview listadapter

I have downloaded an example of SQLite from the following link

Variables as a part of calling a method in python

python function

I'm pretty new to programming and I'm creating a python game for my little sister.