Search code examples
javatestingautomationautomated-tests

Getting a java.lang.ClassNotFoundException: org.testng.TestNG Error


I'm trying to execute my testng.xml file from main method using the code below. But getting a NoClassDefFoundError. Any idea what may have caused this issue?

    public static void main( String[] args )
{
    TestNG testng = new TestNG();
    List<String> suites = Lists.newArrayList();
    suites.add("testng.xml");
    testng.setTestSuites(suites);
    testng.run();
}

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
        at com.automation.app.App.main(App.java:21)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

Solution

  • I was able to fix this issue by using "maven-assembly-plugin" and "maven-dependency-plugin", Then creating the manifest.mf file in resources/meta-inf folder and adding the following information.

    Manifest-Version: 1.0
    Class-Path: .
    Main-Class: {PROVIDE THE MAIN CLASS HERE}