Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new WebDriver causes StackOverflowError

I'm just trying to get setup and have the ability to run the example from Selenium's website. However I've narrowed it down to the FirefoxDriver constructor causing a StackOverflowError. I get the same behavior with InternetExplorerDriver, but not HtmlUnitDriver.

The following code

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest {

    @Test
    public void test() {
        WebDriver driver = new FirefoxDriver();
    }
}

Produces the following stacktrace:

java.lang.StackOverflowError
  at java.lang.Exception.<init>(Unknown Source)
  at java.lang.reflect.InvocationTargetException.<init>(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.json.JSONObject.populateMap(JSONObject.java:937)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  at org.json.JSONObject.wrap(JSONObject.java:1539)
  at org.json.JSONObject.populateMap(JSONObject.java:939)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  at org.json.JSONObject.wrap(JSONObject.java:1539)
  at org.json.JSONObject.populateMap(JSONObject.java:939)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  at org.json.JSONObject.wrap(JSONObject.java:1539)
  at org.json.JSONObject.populateMap(JSONObject.java:939)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  at org.json.JSONObject.wrap(JSONObject.java:1539)
  at org.json.JSONObject.populateMap(JSONObject.java:939)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  at org.json.JSONObject.wrap(JSONObject.java:1539)
  at org.json.JSONObject.populateMap(JSONObject.java:939)
  at org.json.JSONObject.<init>(JSONObject.java:272)
  :
  :

I'm using selenium-java-2.22.0 and the json jar that was packaged with the download (which is json-20080701.jar)

Also of note, when running the new FirefoxDriver, Firefox does launch and you see a new tab page. With InternetExplorerDriver, no window opens, but it produces the same stacktrace with JSONObject looping infinitely. I'm running Firefox 12.0 and IE9 on Windows 7.

like image 314
Joshua Clark Avatar asked May 31 '12 20:05

Joshua Clark


People also ask

What causes StackOverflowError?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

Which of the following expressions is most likely to cause a StackOverflowError?

The most common cause of StackOverFlowError is excessively deep or infinite recursion. In Java: There are two areas in memory the heap and stack.

What causes Java Lang StackOverflowError?

The error java. lang. StackOverflowError is thrown to indicate that the application's stack was exhausted, due to deep recursion i.e your program/script recurses too deeply.


2 Answers

Welcome to JAR hell

Some of the JAR files in your classpath are conflicting with Selenium dependencies. It is kinda strange that it happened even though you are using Ivy. One of your dependencies most likely includes the conflicting classes inside its jar file - or your dependencies need two different versions of the same library.

Anyway, for future user reading this - use some dependency manager to do the hard work with jars for you. Don't try to maintain your libraries manually if you have more that 10 projects with dependencies - you'll most likely screw it up soon. This is quite a reasonable read on dependency solutions, follow some of the links there, don't be lazy. Dependency managers take some time to master, they are a world for themselves. But they help a lot.

Don't use multiple versions of the same library. And if you use multiple libraries from which two use a different version of the same thing ... good luck to you!

Other than that ... our only hope is Java Module System which will be introduced in Java 8 Java 9.

like image 89
Petr Janeček Avatar answered Sep 29 '22 18:09

Petr Janeček


We are hitting this as well, and I can confirm that you encounter this if your test project has org.json classes of other/older versions than the ones WebDriver needs (20080701) somewhere on its classpath.

PS: FTR I've opened http://code.google.com/p/selenium/issues/detail?id=4123 "by mistake", so just to clarify that this is NOT A WEBDRIVER ISSUE.

like image 39
vorburger Avatar answered Sep 29 '22 17:09

vorburger