0

So I am trying to open a chrome browser in incognito mode and then do a few more things beyond that with selenium.

Here is the code and I am using selenium 4.8.1

  package com.bocc.mavenbocc;

  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.chrome.ChromeOptions;

  public class Application {

  public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "assets/drivers/chromedriver.exe");
    
    ChromeOptions options = new ChromeOptions();
    
    options.addArguments("incognito");
    
    WebDriver driver = new ChromeDriver(options);
    
    driver.get("https://www.google.com");
}

}

so first off I get the following:

Error: Unable to initialize main class com.bocc.mavenbocc.Application Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

So then I found that you need to define the dependencies so I placed this in the pom.xml file

<dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-api</artifactId>
     <version>4.8.1</version>
     <scope>test</scope>
</dependency>

but this still results in the same error.

what am I missing.

programmerNOOB
  • 121
  • 3
  • 19

1 Answers1

0

This error message...

Error: Unable to initialize main class com.bocc.mavenbocc.Application Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

...implies that the Java Virtual Machine is unable to find the WebDriver class at runtime which was available at compile time. As an example, if the JVM have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.


This usecase

The error you are seeing clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available. Possibly the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.

This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK / Maven / Gradle.


Solution :

Here are a few steps to solve NoClassDefFoundError :

  • While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.

  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.

  • While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.

    org.seleniumhq.selenium selenium-java 4.8.1
  • Remove the unwanted other <dependency> from pom.xml.

  • While you execute a Maven Project always do

    • maven clean
    • maven install
    • maven test.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352