java 错误无法找到或加载主类 org.testng.TestNG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28889245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:21:15  来源:igfitidea点击:

error Could not find or load main class org.testng.TestNG

javatestng

提问by Eric Nguyen

I am following this tutorial http://www.tutorialspoint.com/testng/testng_environment.htm

我正在关注本教程http://www.tutorialspoint.com/testng/testng_environment.htm

When I enter the command:

当我输入命令时:

java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

I get the error below:

我收到以下错误:

Error: Could not find or load main class org.testng.TestNG

Anyone know why? much appreciated.

有谁知道为什么?非常感激。

回答by Fab738

I encountered exactly the same issue, using the same tutorial today. I was happy to discover I was not alone having the same problem! Anyway, I tried the solution posted on this other post and it worked for me: could not find or load main class org.testng.TestNG

我遇到了完全相同的问题,今天使用相同的教程。我很高兴地发现并非只有我有同样的问题!无论如何,我尝试了在另一篇文章中发布的解决方案,它对我有用找不到或加载主类 org.testng.TestNG

In other word, the working command line for me is:

换句话说,我的工作命令行是:

java -cp ".;C:\TestNG\testng-6.8.jar" org.testng.TestNG testng.xml

Hope this will help you!

希望能帮到你!

回答by KrishPrabakar

Add testng-<version>.jarto the -cpargument.

添加testng-<version>.jar-cp参数。

回答by Shivangshu Nag

I have been working on Ubuntu 15.04 and faced a similar problem. I used maven to install testng. The advantage was it installed all the dependencies along with it. If you are not using maven, then along with testng jar, download jcommander and beanshell jar files as well.

我一直在 Ubuntu 15.04 上工作并遇到了类似的问题。我使用 maven 来安装 testng。优点是它安装了所有依赖项。如果您不使用 maven,那么除了 testng jar 之外,还要下载 jcommander 和 beanshell jar 文件。

When executing the class files after compilation provide the classpath of the following:

编译后执行类文件时,提供以下类路径:

  1. testng jar
  2. jCommander jar
  3. beanshell jar
  1. 测试罐
  2. jCommander jar
  3. 豆壳罐

This is the command I used on my system to run the same example code as mentioned on TutorialsPoint:

这是我在系统上使用的命令,用于运行 TutorialsPoint 上提到的相同示例代码:

java -cp /home/phantom/TestNg\ tutorialsPoint/Programming1:/usr/share/maven-repo/org/beanshell/bsh/2.0b4/bsh-2.0b4.jar:/usr/share/maven-repo/com/beust/jcommander/1.35/jcommander-1.35.jar:/usr/share/maven-repo/org/testng/testng/6.9.5-SNAPSHOT/testng-6.9.5-SNAPSHOT.jar org.testng.TestNG testng.xml

Programming1 is the directory where my .javaand .classfiles are stored.

Programming1 是存放 my.java.classfiles的目录。

回答by bayaprakash Reddy

Two ways are available to solve above problem.In Below mentioned answers change path according to your setup.Before executing the below command in command prompt make sure all selenium jars and TestNG jars should be in the lib folder.

有两种方法可以解决上述问题。在下面提到的答案中,根据您的设置更改路径。在命令提示符下执行以下命令之前,请确保所有 selenium jar 和 TestNG jar 都应在 lib 文件夹中。

First way:java -cp C:\Users\urscomputer\workspace\JenkinsIntegration\lib*;C:\Users\urscomputer\workspace\JenkinsIntegration\bin; org.testng.TestNG testng.xml

第一种方式:java -cp C:\Users\urscomputer\workspace\JenkinsIntegration\lib*;C:\Users\urscomputer\workspace\JenkinsIntegration\bin; org.testng.TestNG testng.xml

Second way:(Provide space between set and classpath)

第二种方式:(在set和classpath之间提供空间)

  1. set classpath=C:\Users\urscomputer\workspace\JenkinsIntegration\lib*;C:\Users\urscomputer\workspace\JenkinsIntegration\bin;
  2. java org.testng.TestNG testng.xml
  1. set classpath=C:\Users\urscomputer\workspace\JenkinsIntegration\lib*;C:\Users\urscomputer\workspace\JenkinsIntegration\bin;
  2. java org.testng.TestNG testng.xml

回答by Mohasin Gothe

It seems to be issue with JUnit dependency/jars. Change your junit version to 3.8.1 and build your workspace, it will work

这似乎是 JUnit 依赖项/jar 的问题。将您的 junit 版本更改为 3.8.1 并构建您的工作区,它将起作用