【问题标题】:NoClassDefFoundError while running a valid jar (compiled with dependencies) despite having commons-httpclient and httpcomponents dependencies on pom尽管在 pom 上具有 commons-httpclient 和 httpcomponents 依赖项,但在运行有效 jar(使用依赖项编译)时出现 NoClassDefFoundError
【发布时间】:2014-07-14 16:14:37
【问题描述】:

我正在尝试通过使用 main 方法中的 selenium webdriver 来自动化一个简单的用户行为(不在测试范围内) 从编译器运行以下代码时,它可以工作! 但是在几种情况下运行 jar 时 - 面临以下问题 (我在 Ubuntu 上运行,使用 java 7)

“线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/http/conn/HttpClientConnectionManager”

@日志 公共类 MainProgram {

public  WebDriver driver = new FirefoxDriver();

public static void main(String args[]) {
 //   Injector injector = Guice.createInjector(new WebModule());

    System.out.println("Browser will soon be opened");
    MainProgram mainProgram = new MainProgram();
    mainProgram.run();

}

public void run(){

    driver.get("http://www.google.co.il");
    WebElement lookFor = driver.findElement(By.name("q"));

    if(!lookFor.isDisplayed()){
        driver.close();
      log.log(Level.WARNING,"Failed!");
    };
    driver.close();

}

}

WebDriver 对 pom 的依赖:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.42.2</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.42.2</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>2.42.2</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.42.2</version>
    </dependency>

Case A

 when removed -commons-httpclient - received: HttpClientConnectionManager as follows:

<!--
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>-->

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.4</version>
          <!--  <scope>test</scope>-->
        </dependency>


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:99)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:82)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:77)

-------------------------------------------------------------------------------------------------------------------------------------------
Case B

removed both commons-httpclient + httpcomponents received HttpClientConnectionManager:

<!--        &lt;!&ndash;
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>&ndash;&gt;

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.4</version>
          &lt;!&ndash;  <scope>test</scope>&ndash;&gt;
        </dependency>-->


liron@liron-Latitude-3330:~$ java -jar automatic-tests-4.0-SNAPSHOT-jar-with-dependencies.jar
Try
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:99)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:82)

---------------------------------------------------------------------------------------------------------------------------------------------

Case C
when both were added to pom - same HttpClientConnectionManager


liron@liron-Latitude-3330:~$ java -jar automatic-tests-4.0-SNAPSHOT-jar-with-dependencies.jar
Browser will soon be opened
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:99)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:82)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:77)


----------------------------------------------------------------------------------------------------------------------------------------------

【问题讨论】:

  • 你是如何尝试运行 JAR 的?你正在运行什么命令?
  • 自从我使用过 - maven-assembly-plugin + seleniumExec.MainProgram (maven-jar-plugin 2.2) 我正在运行:java -jar automatic-tests- 4.0-SNAPSHOT-jar-with-dependencies.jar(不需要运行为“java -cp”+主类名

标签: java maven selenium-webdriver apache-httpclient-4.x


【解决方案1】:

昨晚我的 WebDriver 项目遇到了同样的问题,经过一番调试,发现它缺少以下依赖项。添加它们后,我没有再次遇到此异常。

   <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.3.5</version>
   </dependency>

【讨论】:

  • 酷,我只使用以下(作品)-org.apache.httpcomponentshttpclient4.3.4
  • 我不明白为什么webdriver项目不包含这个?或者更确切地说,我会遵循@SiKing 的回答,并重申不需要明确定义这些依赖项。
【解决方案2】:

您的 pom 依赖项中应该只需要 selenium-java。请参阅this graphic @ Selenium HQ,它解释了 Selenium 的各个部分是如何相关的。此外,Selenium 本身依赖于httpclient,您不需要显式定义它们。如果您确实对这些有正当需求,那么事情就会发生冲突,您需要使用exclusions 进行清理。

清理完 pom 后,您可以运行 mvn dependency:tree 来查看您的项目中发生了什么。

【讨论】:

  • 我设法注释掉 selenium-api + selenium-server 并且项目确实编译了但是在注释掉我收到的 firefox 驱动程序时 - 错误:包 org.openqa.selenium 不存在,+我已经注释掉了与 apa-http 相关的内容,这很好,但我原来的问题没有解决:(
  • 发布mvn dependency:tree的结果。
  • 我不能全部添加它,太长了..树中与http相关的jar是:在selenium-java下:org.apache.httpcomponents:httpmime:jar:4.3.2, org .apache.httpcomponents:httpclient:jar:4.3.2, org.apache.httpcomponents:httpcore:jar:4.3.1 就是这样,
  • UPDATE 我删除了一些来自项目父级并重建的更多依赖项,现在浏览器确实打开了,但得到了 - java.lang.NoClassDefFoundError: com/google/ common/net/MediaType at org.openqa.selenium.remote.http.JsonHttpCommandCodec.encode(JsonHttpCommandCodec.java:197),我去调试一下,谢谢。
  • 通过添加解决 - com.google.guavaguava16.0.1依赖> 工作!
【解决方案3】:

添加新的 google guava 对我有帮助:

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>21.0</version>
</dependency>

那是因为其他依赖可以下载旧的 guava 18 版本。
当然就像 testphreak 说的:org.apache.httpcomponents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2019-05-24
    • 2019-05-02
    • 2021-11-23
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多