Ant + Testng: Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/Parame



test(在project-tests.jar中存在(:

package sample;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.Test;
import utils.Base;
public class SampleTest {
    @Test
    public void Test() {
        log.info("Sample Test called successfully.");
        log.info("Invoking Base Method");
        Base b = new Base();
        b.testMethod();
    }
}

util(project.jar中(:

package utils;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Map.Entry;
public class Base {
    public void testMethod() {
        log.info("Test method in utils.Base class called.");
    }
}

假设在两个类中都创建了日志和可用。

build.xml:

<project default="TestNGSimpleTest">
    <path id="main_cp">
        <pathelement location="dependency/testng-6.9.10.jar"/>
        <pathelement location="dependency"/>
        <pathelement location="."/>
    </path>
    <taskdef name="testng" classpathref="main_cp"
             classname="org.testng.TestNGAntTask" />
    <target name="test">
        <testng classpathref="main_cp" >
            <classfileset dir="." includes="sample/*.class"/>
        </testng>
    </target>
</project>

目标目录结构:

./dependency/{all-dependencies}.jar
./project.jar
./project-tests.jar
./build.xml

{全依赖性}包括:

bsh-1.3.0.jar
bsh-2.0b4.jar
commons-beanutils-1.7.0.jar
commons-beanutils-core-1.8.0.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-1.8.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
jcommander-1.48.jar
testng-6.9.10.jar

当我从IDE运行采样时,它运行良好,但是当我尝试使用ANT(ANT -V测试(运行它时,我点击了以下错误:

[testng] The ' characters around the executable and arguments are
[testng] not part of the command.
[testng] Error: A JNI error has occurred, please check your installation and try again
[testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
[testng]    at java.lang.Class.getDeclaredMethods0(Native Method)
[testng]    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
[testng]    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
[testng]    at java.lang.Class.getMethod0(Class.java:3018)
[testng]    at java.lang.Class.getMethod(Class.java:1784)
[testng]    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
[testng]    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
[testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
[testng]    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[testng]    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[testng]    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[testng]    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[testng]    ... 7 more
[testng] The tests failed.

我认为问题是build.xml(因为我可以在IDE中运行它(,但无法弄清楚为什么此错误(java.lang.noclassdeffounderror:com/beust/beust/jcommander/parameterexception(以及如何修复它。我是否错误地指定了classfileset?我如何进行此运行?

遍历了其他线程,这些线程表明testng jar可能会损坏,没有jcommander jar,无效的classpath-尝试了它们,但没有帮助。

要直接回答问题,似乎您必须包括

<pathelement location="dependency/jcommander-1.48.jar"/>

main_cp中。

这个问题重复一个较旧的突然无法从蚂蚁([testng]引起的:java.lang.classnotfoundexception:com.beust.jcommander.parameterexception(的testng测试。

的确,似乎由于Sonatype快照或您使用github/graddle distrib产生的罐子,似乎不包括很长的jcommander。

因此,您必须将自己添加到路径中的jcommander jarfile传递给 testng ant标签。

您的JCommander版本似乎旧了,声明测试任务的新推荐方法是:

<taskdef resource="testngtasks" classpath="testng.jar" />

相关内容

最新更新