web-dev-qa-db-ja.com

pomにcommons-httpclientおよびhttpcomponentsの依存関係があるにもかかわらず、有効なjar(依存関係でコンパイルされた)の実行中にNoClassDefFoundError

私はメインメソッドからSelenium Webdriverを使用して簡単なユーザー操作を自動化しようとしています(テストスコープの下ではありません)コンパイラーから次のコードを実行すると機能します!しかし、いくつかのケースでjarを実行すると-次の問題に直面します(私はUbuntuで実行していますJava 7を使用)

「スレッド「メイン」の例外Java.lang.NoClassDefFoundError:org/Apache/http/conn/HttpClientConnectionManager」

@Logパブリッククラス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();

}

}

Pomに対するWebDriverの依存関係:

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


----------------------------------------------------------------------------------------------------------------------------------------------
12
liron_hazan

私は昨晩WebDriverプロジェクトでこの同じ問題に遭遇し、少しデバッグした後、次の依存関係が欠けていることがわかりました。それらを追加した後、私は再びこの例外に遭遇しませんでした。

   <dependency>
       <groupId>org.Apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.3.5</version>
   </dependency>
28
testphreak

Pom依存関係ではSelenium-Javaのみが必要です。 このグラフィック@ Selenium HQ を参照してください。これは、Seleniumの各部分がどのように関連しているかを説明しています。さらに、Selenium自体はhttpclientに依存しているため、明示的に定義する必要はありません。それらの正当なニーズがある場合は、物事が衝突し、exclusionsでクリーンアップする必要があります。

Pomをクリーンアップしたら、mvn dependency:treeを実行して、プロジェクトで何が行われているかを確認できます。

4
SiKing

私の場合、新しいグアグアバを追加すると役立ちます:

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

これは、他の依存関係が古い1つのguava 18バージョンをダウンロードできるためです。
そしてもちろんtestphreakのように言った:org.Apache.httpcomponents

3
RamChandra Ali