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)
"Exception in thread "main" 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 dependencies on 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)
----------------------------------------------------------------------------------------------------------------------------------------------
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 testphreak said: org.apache.httpcomponents
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.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
You should only need selenium-java
in your pom dependencies. See the this graphic @ Selenium HQ which 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
.
After you clean up your pom, you can run mvn dependency:tree
to see what is going on in your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With