'Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec

Is this error because of dependencies? I inspected similar problems at Stack Overflow, but didn't came up with an idea how to fix it.

Here is my code:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class base {
    
    public static AndroidDriver<AndroidElement> Capabilities() throws MalformedURLException {
        
        File appdir=new File("src");
        File app=new File(appdir,"ApiDemos-debug.apk");
        
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.DEVICE_NAME,"Emulator1");
        cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiautomator2");
        cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
        AndroidDriver<AndroidElement> driver=new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
        return driver;
    }

This is what I see after running the test:

    авг 13, 2019 11:26:55 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:262)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:37)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:86)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:96)
    at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:94)
    at base.Capabilities(base.java:22)
    at basics.main(basics.java:12)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 14 more


Solution 1:[1]

You're suffering from a form of a Jar Hell due to:

  1. Appium Java Client 7.0.0 has Selenium Java 3.141.59 as a part of its transitive dependencies
  2. You explicitly declare Selenium Java Client 3.12.0 as your project dependency
  3. Java Classloading picks up the wrong classes with not matching APIs and dependencies - that's why you're getting this error

The solution is to remove Selenium Java Client 3.12.0 from your project dependencies/classpath, Maven will resolve the required dependencies on its own.

See Appium - Code Examples - Java article for more information and sample projects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T