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

时间:2012-10-29 11:25:21

标签: 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;

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

整个堆栈跟踪是:

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)

我用Google搜索了这个错误,它说了一些与类路径相关的内容。但我不确定究竟要做什么。我对这个领域很新。所以任何帮助都将深表感谢。 提前谢谢。

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

如果您使用的是IntelJ IDE。确保您在项目详细信息中提供正确的JDK。你可以通过
检查它 按Ctrl + Alt + Shift + S - &gt;将打开项目设置。 点击项目。您可以看到在那里指定了哪个JDK。 Project Setting page 转到系统中的JDK路径。如果您使用的是Windows 64位版本。它将是

C:\ Program Files \ Java \
确保您指定的JDK在相同路径中也具有相应的jre(JDK,JRE版本应该相同)。 JDK In my system

所以你可以看到我有jdk1.8.0_141,jre1.8.0_141。我已将此jdk1.8.0_141索引到我的IntelJ 当您检查时如果您看到相应的JRE不存在。 IntelJ索引不合适,导致您在问题中提到的此类错误 您可以从http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新的JDK 如果您在Windows上使用.exe版本的JDK并在您的系统中运行。这将自动在同一路径中安装corresponsind jre。 你可以对评论表示怀疑。会尽力帮助你。

之后回到IntelJ。转到项目设置 - &gt;项目。单击新建,然后选择并添加正确的JDK。点击确定。然后转到你的程序。你可以看到IntelJ正在重新索引你的东西。可能需要几分钟。之后尝试重新运行程序。

答案 2 :(得分:0)

我也有同样的错误,我很难解决。

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

我的类路径上有两个具有相同“完全限定”名称的类

  

org.apache.http.conn.scheme.Scheme

Java只是选择了看到的第一个Scheme类,这是错误的。它使用没有我期望的方法的Scheme类。

好吧,所以我通过在Maven中声明正确的依赖关系来解决问题?否。两个Scheme类都位于不同的工件中,一个是传递依赖,因此无法完全删除。

解决方案,我们必须通过将要使用的Scheme类的Jar放入Tomcat来手动覆盖Maven。由Tomcat容器拾取的依赖项将在Maven依赖项之前加载。因此,请将Scheme类Jar加载到您的Web容器中,以确保可以首先找到它。

这些命令在分析时非常有用。     mvn依赖性:analyze -o     mvn依赖项:tree -o