Java 无法解析类引用 org/openqa/selenium/WebDriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22064853/
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-08-13 13:21:06  来源:igfitidea点击:

unable to resolve class reference org/openqa/selenium/WebDriver

javaeclipseseleniumjar

提问by testing

In the directory C:\jars>In the Jars folder I have Test folder. Inside the Test folder I have OneReports.jar,testng.jar, TestngTestSuiteUsingXML.javaand Test.XML

在目录中C:\jars>在 Jars 文件夹中,我有 Test 文件夹。测试文件夹里面我有OneReports.jartestng.jarTestngTestSuiteUsingXML.javaTest.XML

The OneReports.jarI have exported from eclipse and it contains a few java Projects.

OneReports.jar我从Eclipse的出口,它包含了一些Java项目。

The Test.xmlI have created testsuite and that file is

Test.xml我创建测试包和文件

In the command Prompt I tried to run:

在命令提示符中,我尝试运行:

  1. C:\Jars>javac -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng .jar" test\TestngTestSuiteUsingXML.java

  2. C:\Jars>java -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng. jar" test.TestngTestSuiteUsingXML

  1. C:\Jars>javac -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng .jar" test\TestngTestSuiteUsingXML.java

  2. C:\Jars>java -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng.jar" test.TestngTestSuiteUsingXML

Here I got error as follows:

在这里我得到如下错误:

[[TestNGClassFinder]] Unable to read methods on class test.LoginOneReports - unable to resolve class reference org/openqa/selenium/WebDriver
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetPublicMethods(Unknown Source)
    at java.lang.Class.getMethods(Unknown Source)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:59)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:153)
    at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:522)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1289)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at test.TestngTestSuiteUsingXML.main(TestngTestSuiteUsingXML.java:20)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 18 more

 C:\Jars>

Could any one please help me to fix the issue. I'm not using maven. Using Selenium webdriver, TestNG, Java for writing code.

任何人都可以帮我解决这个问题。我没有使用maven。使用 Selenium webdriver、TestNG、Java 编写代码。

采纳答案by Jayan

Since your javac worked, you have testng.jar in the classpath correctly. Then the program has dependency on other libraries selenium etc. It will be correct to use individual jars instead of wild cards.

由于您的 javac 工作正常,您在类路径中正确地拥有 testng.jar。然后程序依赖于其他库 selenium 等。使用单独的 jars 而不是通配符是正确的。

java -cp "C:\Program Files\Java\selenium<version>.jar;OneReports.jar;testng.jar" test.TestngTestSuiteUsingXML

This may give more classnotfound exception. Add only those to your classpath.

这可能会给出更多 classnotfound 异常。仅将这些添加到您的类路径中。

Note that you are referring other jars too by relative path (OneReports.jar or testng.jar). This can get pretty messy. Consider using ant/maven/gradle to do this part of the work. They would form the classpath more reliably.

请注意,您也通过相对路径(OneReports.jar 或 testng.jar)引用了其他 jar。这可能会变得非常混乱。考虑使用ant/maven/gradle来做这部分工作。它们会更可靠地形成类路径。