gpt4 book ai didi

java - 尽管在 pom 上有 commons-httpclient 和 httpcomponents 依赖,但在运行有效的 jar(使用依赖项编译)时出现 NoClassDefFoundError

转载 作者:搜寻专家 更新时间:2023-10-30 20:56:32 25 4
gpt4 key购买 nike

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

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

@日志公共(public)类 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>

关于java - 尽管在 pom 上有 commons-httpclient 和 httpcomponents 依赖,但在运行有效的 jar(使用依赖项编译)时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24741012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com