Ant + Testng:线程中的异常" main" java.lang.NoClassDefFoundError:com / beust / jcommander / ParameterException

时间:2017-04-18 20:14:23

标签: ant testng

测试(出现在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

{all-dependencies}包括:

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运行SampleTest时它运行正常,但是当我尝试使用ant(ant -v test)运行它时,我遇到以下错误:

[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 / jcommander / ParameterException)以及如何修复它。我是否错误地指定了classfileset?我该怎么做呢?

通过其他线程表明testng jar可能已损坏,没有jcommander jar,无效的类路径 - 试过但没有帮助。

1 个答案:

答案 0 :(得分:1)

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

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

这个问题重复了较早的Suddenly can't run TestNG tests from ant ([testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException),其答案似乎已经过时了。

实际上,似乎很久以来,jcommander既不包含在sonatype快照中,也不包含在github / graddle distrib中生成的jar。

因此,您必须在传递给testng ant标记的路径中添加jcommander jar文件。

您的jcommander版本看起来很旧,并且声明testng任务的新recommended way是:

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