While running a simple Selenium test in Java, the program fails with the following error:
Error: Unable to initialize main class First
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
The project uses Selenium Java 4.1.3 and follows the setup from a JavaTpoint tutorial.
The WebDriver class should be available in this version, but Eclipse throws an error during runtime even though there are no compilation issues.
Sample code:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
public class First {
public static void main(String args) {
System.setProperty(“webdriver.chrome.driver”, “/root/drivers/chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.navigate().to(“http://www.google.com/”);
driver.findElement(By.id(“lst-ib”)).sendKeys(“javatpoint tutorials”);
driver.findElement(By.name(“btnK”)).click();
}
}
This issue typically occurs when the Selenium JARs are not correctly added to the runtime classpath.
Even if the dependencies exist in the IDE, the compiled program might not have access to them at execution.