Java 尽管在 pom 上具有 commons-httpclient 和 httpcomponents 依赖项,但在运行有效 jar(使用依赖项编译)时出现 NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24741012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoClassDefFoundError while running a valid jar (compiled with dependencies) despite having commons-httpclient and httpcomponents dependencies on pom
提问by liron_hazan
I'm trying to automate a simple user act by using selenium webdriver from main method (not under test scope) When running the following code from the complier it works! But when running the jar on several cases - facing the following issue (I'm running on Ubuntu, using java 7)
我正在尝试通过使用来自 main 方法(不在测试范围内)的 selenium webdriver 来自动化一个简单的用户行为当从编译器运行以下代码时,它可以工作!但是在几种情况下运行 jar 时 - 面临以下问题(我在 Ubuntu 上运行,使用 java 7)
"Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager"
“线程“main”java.lang.NoClassDefFoundError 中的异常:org/apache/http/conn/HttpClientConnectionManager”
@Log public class MainProgram {
@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();
}
}
WebDriver dependencies on pom:
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:
<!-- <!–
<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>-->
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)
----------------------------------------------------------------------------------------------------------------------------------------------
采纳答案by testphreak
I ran into this same issue last night with my WebDriver project, and after a bit of debugging, found out that it was missing the following dependency. After adding them I didn't encounter this exception again.
我昨晚在我的 WebDriver 项目中遇到了同样的问题,经过一些调试后,发现它缺少以下依赖项。添加它们后,我没有再次遇到此异常。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
回答by SiKing
You should only need selenium-java
in your pom dependencies. See the this graphic @ Selenium HQwhich explains how parts of Selenium are related. Further, Selenium itself has dependencies on httpclient
, you should not need to define those explicitly. If you do have a legitimate need for those, things will collide and you will need to clean that up with exclusions
.
您应该只需要selenium-java
在您的 pom 依赖项中。请参阅此图 @ Selenium HQ,它解释了 Selenium 的各个部分是如何相关的。此外,Selenium 本身依赖于httpclient
,您不需要显式定义它们。如果您确实对这些有合法需求,事情就会发生冲突,您需要使用exclusions
.
After you clean up your pom, you can run mvn dependency:tree
to see what is going on in your project.
清理完 pom 后,您可以运行mvn dependency:tree
以查看项目中发生了什么。
回答by RamChandra Ali
Adding new google guava helps in my case:
添加新的谷歌番石榴对我有帮助:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
That's because other dependencies can download old one guava 18 version.
And of course like testphreaksaid: org.apache.httpcomponents
那是因为其他依赖项可以下载旧的 guava 18 版本。
当然就像testphreak说的:org.apache.httpcomponents