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.
I’ve faced this issue before. Even if your IDE (Eclipse) compiles fine, this error usually happens because the Selenium JARs aren’t on the runtime classpath.
Make sure you either:
Add all Selenium JARs and dependencies to your project’s Build Path → Libraries → Add External JARs.
If running from the command line, include the JARs in the -cp argument:
java -cp ".;selenium-java-4.1.3.jar;libs/*" First
Without this, the JVM won’t find org.openqa.selenium.WebDriver at runtime.
From my experience, this happens a lot when using Maven or manually downloaded JARs.
Selenium Java 4+ splits dependencies into multiple JARs (like selenium-api, selenium-chrome-driver, selenium-support, etc.).
Just adding selenium-java-4.1.3.jar isn’t enough, you need all the required JARs in the classpath, or better, manage them with Maven/Gradle so dependencies are handled automatically.
I ran into the same NoClassDefFoundError when I was following tutorials.
One trick that worked for me was creating a lib folder in the project, putting all Selenium JARs there (and their dependencies), and then making sure Eclipse includes them in both Compile and Runtime classpath.
Once that was done, the error disappeared and the test ran fine.