java - "Could not find main class"在 Eclipse 中运行 TestNG 脚本时出错

标签 java eclipse selenium-webdriver testng

我正在尝试在 Eclipse 中运行 Selenium Web-Driver TestNG 脚本。我收到如下所示的错误。

TestNG run error

我尝试过更改首选项、选中/取消选中“使用项目 testNG jar”、弄乱构建路径并重新创建项目。任何人都可以帮我解决这个问题,以便我可以继续使用 TestNG 创建很棒的脚本吗?

最佳答案

您的问题,基于不支持的major.minor版本51.0是错误的Java版本。您正在使用的库是在一个版本中编译的,而您正在使用另一个版本(很可能或肯定)您正在使用较低版本。

版本 51.0 指的是 Java 7。我想说您正在使用的库是 Java 7,而您的库版本较低...要更改此设置,请转到:

Window -> Preferences -> Java -> Compiler 

并将编译器合规级别设置得更高,至少1.7(即Java 7)。如果这不是一个选项,请安装较新的 Java JDK(首选 1.8,如果您愿意,也可以编译为 Java 7)。

不支持的主要.次要版本 5{1,2,..}.0 (复制自https://stackoverflow.com/a/11432195/928952)

J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

更新 JDK Eclipse 的更详细解释是使用:

project -> properties -> java build path-> Add Library -> JRE System Library-> Installed JREs -> Add(unless jdk* is already in the list, then select that and go to next post -> standard VM -> Directory button -> browse to the location where you installed the JDK. Inside that directory you see a couple of folders, including bin and lib.
Make sure you select the parent directory of them. Click OK. You'll then see a list of .jars under JRE system libraries (must!) -> hit finish. Select the checkbox next to jdk1.8*. Press OK then finish.
Now you've got the right Java version in the Build Path... Now we have to make sure that becomes the compiler Now we're back at the properties, Go to Java Compiler, make sure enable project specific settings is turned off. Click Configure Workspace Settings.... set the Compliance level: 1.8 -> apply (you get a message on compiler settings changed rebuild blablabla hit yes) then ok, then ok again...

Eclipse 在找不到库时出现奇怪的错误

You can try deleting the .project and .classpath files in the root of your project. Delete the project from workspace (don't delete your source, backup first)... then reimport the project... might be that eclipse has configured something that doesn't exist anymore... helps me fix some dubious errors...

关于java - "Could not find main class"在 Eclipse 中运行 TestNG 脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705725/

相关文章:

java - 使用来自 Team Build Extensions 2010 的 ANT 构建 Java

java - 方法中每次运行新值

java - 如何将进度监视器传递给多个子程序?

java - 有没有办法在不通知文档它由 WebDriver 控制的情况下使用 Selenium WebDriver?

java - Selenium Java 代码打印 google 搜索的前五个结果

python - 如何让 selenium 和 chromium 在 ubuntu 上工作

java - session 销毁后仍获取 session ID

java - 您是否应该使用 AccountManager 来存储 Android 应用程序的用户名和密码?

java - 无法将我的 .java 转换为 .class

java - 当 Java 应用程序从 Eclipse 运行时,它应该在哪里创建文件?