Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException"Exception

I wanted to invoke testng programmatically. Not eclipse plug-in.

I have associated "testng-6.8.21.jar" and running through eclipse and i ran below code:

import org.testng.TestNG;

public class SampCls 
{
        public static void main(String[] args)
        {
            TestNG test=new TestNG();
        }
}

Getting below exception. How can i overcome this exception.

Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
    at SampCls.main(SampCls.java:12)
Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    ... 1 more
like image 410
Uday Avatar asked May 25 '15 09:05

Uday


1 Answers

Change:

Class cls = Class.forName("TestSuite.TestCases.AddContactHappyPath").getClass();
test.setTestClasses(new Class[] { cls });

By:

 test.setTestClasses(new Class[] { AddContactHappyPath.class });

All code is

import org.testng.TestNG;
import com.xxx.test.others.AddContactHappyPath;

public class SampCls {
    public static void main(String[] args) throws ClassNotFoundException {
        TestNG test = new TestNG();
         test.setTestClasses(new Class[] { AddContactHappyPath.class });
         test.run();
    }
}

TestNG code is:

import org.testng.annotations.*;

public class AddContactHappyPath {

    @Test()
    public void AddContactHappyPathTest() {
        System.out.println("hello world");
    }
}

Console result:

[TestNG] Running:
  Command line suite

hello world

===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
like image 198
Stéphane GRILLON Avatar answered Oct 31 '22 10:10

Stéphane GRILLON