为什么我会得到java.lang.NoSuchMethodError:即使构建是绿色的?

为什么我会得到java.lang.NoSuchMethodError:即使构建是绿色的?,java,intellij-idea,webdriver,nosuchmethoderror,Java,Intellij Idea,Webdriver,Nosuchmethoderror,我在intellij中使用HtmlUnitDriver编写了一个测试。 代码是: import junit.framework.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class HtmlUnitDemo { WebDriver driver; p

我在intellij中使用HtmlUnitDriver编写了一个测试。 代码是:

import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlUnitDemo {
    WebDriver driver;

    public static void main(String args[]){
        HtmlUnitDemo obj = new HtmlUnitDemo();
        obj.setup();
        obj.openURL();
    }

    public void openURL() {

        driver.get("https://mail.google.com");
        driver.findElement(By.id("username")).sendKeys("xyz");
        driver.findElement(By.id("password")).sendKeys("pqr");
        driver.findElement(By.className("btn-submit")).click();
        Assert.assertEquals(driver.getTitle(), "CAS – Central Authentication Service");
    }

    public void setup(){
        driver = new HtmlUnitDriver();
    }
}
这段代码是在我使用gradle时生成的。但是,当我在IDE中运行它时,它会给我一个

*java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)
*java.lang.NoSuchMethodError:org.apache.http.conn.scheme.scheme(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)
整个堆栈跟踪是:

at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:504)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClient(HttpWebConnection.java:470)
at com.gargoylesoftware.htmlunit.HttpWebConnection.setUseInsecureSSL(HttpWebConnection.java:659)
at com.gargoylesoftware.htmlunit.WebClient.setUseInsecureSSL(WebClient.java:1085)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:263)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:129)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:172)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:168)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.setup(HtmlUnitDemo.java:30)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.main(HtmlUnitDemo.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
位于com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:504)
位于com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClient(HttpWebConnection.java:470)
在com.gargoylesoftware.htmlunit.HttpWebConnection.setuseesecuressl(HttpWebConnection.java:659)上
位于com.gargoylesoftware.htmlunit.WebClient.setUseSecureSSL(WebClient.java:1085)
位于org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:263)
位于org.openqa.selenium.htmlunit.HtmlUnitDriver.(HtmlUnitDriver.java:129)
位于org.openqa.selenium.htmlunit.HtmlUnitDriver。(HtmlUnitDriver.java:172)
位于org.openqa.selenium.htmlunit.HtmlUnitDriver.(HtmlUnitDriver.java:168)
位于smoke.com.thoughtworks.twu.HtmlUnitDemo.setup(HtmlUnitDemo.java:30)
位于smoke.com.thoughtworks.twu.HtmlUnitDemo.main(HtmlUnitDemo.java:14)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:601)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我用谷歌搜索了这个错误,它说了一些与类路径有关的东西。但我不确定到底要做什么。我对这个领域很陌生。因此,我们将非常感谢您的帮助。
提前感谢。

NoSuchMethodError
可能表示类所在的库
org.apache.http.conn.scheme.scheme
的版本与预期的版本不同


检查您正在使用的Selenium库所使用的依赖项。如果它来自maven,那么它可能是一个简单的任务,否则请查看他们的网站。

如果您使用的是IntelJ IDE。确保您在项目细节中给出了正确的JDK。您可以通过
按Ctrl+Alt+Shift+S->将打开项目设置。 点击项目。您可以看到在那里指定了哪个JDK。 转到系统中JDK的路径。如果您使用的是Windows 64位版本。这将是

C:\ProgramFiles\Java\
确保您指定的JDK在相同的路径中也有相应的jre(JDK,jre版本应该相同)。

你可以看到我有jdk1.8.0_141,jre1.8.0_141。我已经将这个jdk1.8.0_141索引到我的IntelJ中。
当您检查是否看到类似的JRE不存在时。IntelJ索引不正确,会导致您在问题中提到的错误。
您可以从
如果您在windows上,请使用.exe版本的JDK并在系统中运行。将自动在同一路径中安装相应的绑定jre。 你可以把你的怀疑放在评论上。我会尽力帮你的


之后再回到IntelJ。进入项目设置->项目。单击新建,选择并添加适当的JDK。单击确定。然后去你的节目。你可以看到IntelJ正在为你的东西重新编制索引。可能需要几分钟。之后,尝试重新运行程序。

我遇到了同样的错误,很难解决

java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)

这意味着您运行的代码版本与正在编译的代码版本不同。需要堆栈跟踪,但依赖项中的某个jar文件的版本似乎不正确。是的,有几种方法可以“实现”这一点,但大约95%的时间意味着您使用一个jar版本编译,并使用不同的版本运行,正如彼得所说。如果您得到两个不匹配的“配对”JAR,您也可以得到它。JDK包括JRE。OP所说的类无论如何都不在JDK/JRE中。请删除或更新。我已检查依赖项。jar正在使用旧版本。它仍然不起作用。我看不出检查能解决问题,否决票。
mvn dependency:analyze -o
mvn dependency:tree -o