Home > Mobile >  java.lang.NoClassDefFoundError Error while using WebDriverManager in Java project
java.lang.NoClassDefFoundError Error while using WebDriverManager in Java project

Time:12-10

I have created a plain Java Project and added all selenium 4.0.1/testng 7 and WebDriverManager 5.0.3 jars and set up classpath. My Code here is

WebDriverManager.chromedriver().setup();
WebDriver driver= new ChromeDriver();

There is no compilation error, however, when execute as TestNG Class getting below error

java.lang.NoClassDefFoundError: org/apache/hc/core5/http/ClassicHttpRequest

at SeleniumSampleTest.checkTest(SeleniumSampleTest.java:15)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

Caused by: java.lang.ClassNotFoundException: org.apache.hc.core5.http.ClassicHttpRequest at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 28 more

When I use the same code but build the project as a maven, then not getting any issues. Any lights on this error will help to move ahead in the Java project Please refer complete code here [https://gist.github.com/veenadevi/62e7098c9809b2f0d2a1baa68141c028][1]

CodePudding user response:

try this way->

    public String baseURL=readconfig.getApplicationURL();
    public static WebDriver driver;
    public static Logger logger;
    
    @Parameters("browser")
    @BeforeClass
    public void setup(String br)
    {
        //log 
        logger=Logger.getLogger("UnifyJobsV1");
        PropertyConfigurator.configure("Log4j.properties");
        
        if(br.equals("chrome")) 
        {
            System.setProperty("webdriver.chrome.driver",readconfig.getChromePath());
            driver=new ChromeDriver();
        }
        else if(br.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver",readconfig.getFirefoxPath());
            driver=new FirefoxDriver();
        }
        driver.get(baseURL);

}
        
    
    @AfterClass
    public void tearDown() {
        driver.quit();
    }

CodePudding user response:

There is com.intellij.rt.testng.IDEARemoteTestNG.run in stacktrace, so you're using Intellij IDEA. Since maven execution works, there is no dependencies conflicts.

It looks like IDE related issue.

I can suggest 2 options.

1 Try to mvn clean, and Build -> Rebuild Project in IDE.

2 Also restarting IDEA and File -> Invalidate caches could help.

Hope something will help you.

  • Related