本文介绍了获取"java.lang.NoClassDefFoundError:com/beust/jcommander/ParameterException"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式调用testng.不是eclipse插件.

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

我关联了"testng-6.8.21.jar"并通过eclipse运行,并且我在以下代码下运行:

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

推荐答案

更改:

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

通过:

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

所有代码均为

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代码为:

import org.testng.annotations.*;

public class AddContactHappyPath {

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

控制台结果:

[TestNG] Running:
  Command line suite

hello world

===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

这篇关于获取"java.lang.NoClassDefFoundError:com/beust/jcommander/ParameterException"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:02