加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – NoClassDefFoundError在运行一个有效的jar(用依赖关系编

发布时间:2020-12-14 05:33:42 所属栏目:Java 来源:网络整理
导读:我试图通过使用主要方法(不在测试范围)下的selenium webdriver自动化一个简单的用户行为 当从编译器运行以下代码时,它可以工作! 但是在几个案例上运行jar时 – 面临以下问题 (我在Ubuntu上运行,使用 java 7) “异常在线程”主要“java.lang.NoClassDefFound
我试图通过使用主要方法(不在测试范围)下的selenium webdriver自动化一个简单的用户行为
当从编译器运行以下代码时,它可以工作!
但是在几个案例上运行jar时 – 面临以下问题
(我在Ubuntu上运行,使用 java 7)

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

@Log
public class 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)


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

解决方法

昨天晚上,我遇到了与我的WebDriver项目相同的问题,经过一番调试,发现它缺少以下依赖关系.添加后我再也没有遇到这个异常.
<dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.3.5</version>
   </dependency>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读