Search code examples
javaandroidseleniumappium

Why is my IDE looking in the wrong dependency folder for a needed class?


I'm trying to run a script that was working fine until I changed the home folder. Once I changed the home folder I ran into a few issues but I was able to resolve them. The only thing I have not been able to resolve is the following error.

    Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/internal/JsonToWebElementConverter
    at BrowserStackAndroid.main(BrowserStackAndroid.java:43)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.internal.JsonToWebElementConverter
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 1 more

This is an interesting error that, after some research, I have not been able to wrap my head around.

The class that it is searching for is in the org.selenium.remote dependency. Except that it's not in the internal folder but the remote folder. So the class that it is searching for is JsonToWebElementConverter.

This class is located in the following path org/openqa/selenium/remote/JsonToWebElementConverter

The folder that the IDE (or Maven?) is looking into is org/openqa/selenium/remote/internal Which only has the class WebElementToJsonConverter

I've tried reinstalling the library via maven in several different ways, using different dependencies, etc but haven't been able to fix this issue.

I've never come across an issue like this before so I'm really excited to see what the solution is going to be.

Notes: The error is being thrown because of the following line of code

AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("https://" + userName + ":" + accessKey + "@hub-cloud.browserstack.com/wd/hub"), caps);

This project worked perfectly fine before but as soon I changed the directory of the project stopped working.


Solution

  • This is happening as you have same class or library with in 2 dependencies. Simply removing selenium based dependencies will resolve the issue as all selenium dependencies already comes with Appium java client. For classpath you can check at java docs

    So the way to set class path via cmd (Navigate to project root folder)

    set CLASSPATH= target/org/openqa/selenium/remote/internal/JsonToWebElementConverter
    

    Note: for maven project all compiled java files should be in target folder. If you are facing same issue you can add missing class inclasspath destination should be target folder as explained in java docs