Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

HtmlUnitDriver Error: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebWindowListener

Ask Question

I created a java class called htmlUnitTest to do a test with Selenium Headless Testing. The class basically goes to the google site and writes a text in the search box of the site, does the search and takes the page title and displays the title in the Eclipse console. When I run the test, Eclipse displays the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebWindowListener at htmldriver.htmlUnitTest.main(htmlUnitTest.java:12) Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.WebWindowListener at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more

The jar file I used to import the HtmlUnitDriver class for my project was: selenium-htmlunit-driver-2.52.0.jar.

Below is the Java code.

public class htmlUnitTest {
public static void main(String[] args) {
    WebDriver driver = new HtmlUnitDriver();
    driver.get("https://www.google.com.br/");
    WebElement ele = driver.findElement(By.name("q"));
    ele.sendKeys("Teste");
    ele.submit();
    System.out.println("Título da página: " +driver.getTitle());
    driver.quit();

The HtmlUnitDriver is only a small wrapper around HtmlUnit itself. So you need all the dependencies of the selenium-htmlunit-driver also (in fact HtmlUnit with all the dependencies).

You can download the whole package here https://github.com/SeleniumHQ/htmlunit-driver/releases

RBRi then just add the htmlunit-driver-2.30.0-jar-with-dependencies.jar file to my project that the Java script will work? – Kakashi - Sensei Apr 12, 2018 at 6:21 No, you have to unpack htmlunit-driver-2.30.0-jar-with-dependencies.jar into some lib folder and then add all the jars as libs to your project. Or you can use some dependency aware build toolchain like ant+ivy, maven or gradle. – RBRi Apr 12, 2018 at 6:55 Sorry my fault, was confused... you are right, use the htmlunit-driver-2.30.0-jar-with-dependencies.jar instead of the driver jar. No need to unpack. Sorry – RBRi Apr 12, 2018 at 18:23

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.