繁体   English   中英

通过命令行运行时,Java类文件无法加载testng类

[英]Java class file failed to load testng class when run through command line

我有这段代码,它从main方法中使用XML文件调用testNG 我试图通过以下命令从命令行调用此类文件:

java -cp My_Automation.jar com.mycomp.test.sanity.InvokeTestNGTest

但是,此操作失败,并显示以下消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener

我尝试通过Eclipse运行此程序,该程序运行良好,但从命令行界面调用时失败。 所有测试JAR都放置在类路径中。 我不明白这个差异。

这是我的代码:

package com.mycomp.test.sanity;

import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.TestListenerAdapter;


public class InvokeTestNGTest {
    public static void main(String[] args) {


        List<String> xmlFileList = new ArrayList();
        xmlFileList.add("ILIO_testNG.xml");
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();

        testng.setTestSuites(xmlFileList);
        testng.addListener(tla);
        testng.run();

        }

}

除非所有的jar文件都在同一目录中,否则您需要在类路径中指定每个jar的路径。 您还需要确保您没有丢失任何其他JAR文件。

java -cp "/path/to/project/My_automation.jar; /path/to/project/libs/testng-5.5.jar" ... 

从命令提示符运行时,请在类路径中提供testng.jar。

java -cp "My_Automation.jar;testng-5.5.jar" com.mycomp.test.sanity.InvokeTestNGTest

谢谢

您的运行时类路径上不包含哪个org.testng.ITestListener类。 Sergii Zagriichuk提到了一个testng.jar?

意识到我已经将其导出为一个jar而不是可执行jar。 这不是为项目选择依赖罐。 谢谢你的指导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM