user2640248
user2640248

Reputation: 393

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();

        }

}

Upvotes: 2

Views: 9526

Answers (4)

user2640248
user2640248

Reputation: 393

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

Upvotes: 0

nitind
nitind

Reputation: 20003

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

Upvotes: 0

JohnP
JohnP

Reputation: 135

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" ... 

Upvotes: 1

saurav
saurav

Reputation: 3462

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

Upvotes: 0

Related Questions