简体   繁体   English

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

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

I have this piece of code, which invokes testNG with an XML file from the main method. 我有这段代码,它从main方法中使用XML文件调用testNG I am trying to invoke this class file from command line via the following: 我试图通过以下命令从命令行调用此类文件:

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

However, this fails with the following message: 但是,此操作失败,并显示以下消息:

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

I have tried running this through Eclipse, which works perfectly fine, but it fails when invoked from the command line interface. 我尝试通过Eclipse运行此程序,该程序运行良好,但从命令行界面调用时失败。 All testing JARs are placed in the classpath. 所有测试JAR都放置在类路径中。 I don't understand the discrepancy. 我不明白这个差异。

Here is my code: 这是我的代码:

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();

        }

}

Unless all your jar files are in the same directory, you'll need to specify the path to each jar in the classpath. 除非所有的jar文件都在同一目录中,否则您需要在类路径中指定每个jar的路径。 You'll also need to make sure you're not missing any other JAR files. 您还需要确保您没有丢失任何其他JAR文件。

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

Please provide the testng.jar in your classpath while running from command prompt. 从命令提示符运行时,请在类路径中提供testng.jar。

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

Thanks 谢谢

Which ever jar contains the org.testng.ITestListener class is not on your runtime classpath. 您的运行时类路径上不包含哪个org.testng.ITestListener类。 Sergii Zagriichuk mentioned a testng.jar? Sergii Zagriichuk提到了一个testng.jar?

Realized that I had exported it as just a jar instead of executable jar. 意识到我已经将其导出为一个jar而不是可执行jar。 This was not picking up the dependency jar's for the project. 这不是为项目选择依赖罐。 Thanks for the pointers 谢谢你的指导

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

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