NoClassDefFoundError при запуске действительного jar (скомпилированный с зависимостями), несмотря на наличие зависимостей commons-httpclient и httpcomponents от pom
Я пытаюсь автоматизировать простой пользовательский акт, используя selenium webdriver из основного метода (не в области проверки)
При запуске следующего кода от complier он работает!
Но при запуске банки в нескольких случаях - перед следующей проблемой
(Я работаю на Ubuntu, используя java 7)
"Исключение в потоке" main "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();
}
}
Зависимости 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>-->
[email protected]:~$ 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
[email protected]:~$ 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)
----------------------------------------------------------------------------------------------------------------------------------------------
Ответы
Ответ 1
Я столкнулся с тем же вопросом прошлым вечером с моим проектом WebDriver, и после небольшой отладки выяснил, что в нем отсутствует следующая зависимость. После их добавления я больше не встречал этого исключения.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
Ответ 2
Вам нужно только selenium-java
в ваших зависимостях pom. См. этот графический @Selenium HQ, в котором объясняется, как связаны части Селена. Кроме того, сам Selenium имеет зависимости от httpclient
, вам не нужно явно определять их. Если у вас есть законная потребность в них, ситуация столкнется, и вам нужно будет очистить ее с помощью exclusions
.
После очистки вашего pom вы можете запустить mvn dependency:tree
, чтобы узнать, что происходит в вашем проекте.
Ответ 3
Добавление нового google-guava помогает в моем случае:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
Это потому, что другие зависимости могут загрузить старую версию guava 18.
И, конечно, как testphreak сказал: org.apache.httpcomponents