How can I resolve my class from a different jar with same structure like another
Asked Answered
M

4

5

How can I resolve my class from a different jar with same structure like another

Note : Though the jars in question contains the word selenium but the question here have no direct relation with selenium

Till a few days back PhantomJSDriver was released bundled along with selenium-server-standalone-v.v.v.jar. So my Class was working fine as:

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class A_PhantomJS
{
    public static void main(String[] args) 
    {
          File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
          System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
          WebDriver driver= new PhantomJSDriver();
          driver.manage().window().maximize();
          driver.get("https://www.google.co.in");
    }
}

Now selenium-server-standalone-v.v.v.jar doesn't bundles the jar for PhantomJSDriver dependency.

So I have downloaded the jar phantomjsdriver-1.1.0.jar and added as an external jar to my project.

You can see the structure of the phantomjsdriver-1.1.0.jar is similar to what it was earlier when it was bundled with selenium-server-standalone-v.v.v.jar

PhantomJSDriver

Now, though my Class gets resolved through:

import org.openqa.selenium.phantomjs.PhantomJSDriver;

But I am facing a Runtime exception of java.lang.NoClassDefFoundError as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/browserlaunchers/Proxies
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:178)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:99)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:89)
    at demo.A_PhantomJS.main(A_PhantomJS.java:15)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 4 more

Line 15 being:

WebDriver driver= new PhantomJSDriver();

As per the error I have searched for org.openqa.selenium.browserlaunchers.Proxies within the phantomjsdriver-1.1.0.jar unable to find any clue.

NoClassDefFoundError

Can anyone help me out please?

Menfolk answered 10/1, 2018 at 17:7 Comment(0)
M
2

Finally the Question is Answered in a User Group by none other than Simon Stewart.

Answer : There's a version of phantomjsdriver ('com.codeborne:phantomjsdriver:jar:1.4.4') that appears to be kept up to date with latest selenium releases. I'd suggest using that.

Here is the snapshot of Simon's comment :

Simon_Stewart

Here is the working solution: PhantomJSDriver_solved

Menfolk answered 18/1, 2018 at 15:10 Comment(0)
A
3

This jar includes org.openqa.selenium.browserlaunchers.Proxies, try adding it to your classpath:

https://search.maven.org/remotecontent?filepath=org/seleniumhq/selenium/selenium-api/2.4.0/selenium-api-2.4.0.jar

If you miss other classes, you can search them by classname with Advanced Search on Maven Central Repository: https://search.maven.org/#advancedsearch%7Cgav

Autoionization answered 12/1, 2018 at 22:42 Comment(3)
Thanks, Does that mean if I don't bundle a particular jar and later add the jar as an external dependency, I may require to add further more dependency jars?Menfolk
It depends on the differences between the original jar with bundled dependencies, and the new jar without bundled dependencies: do you know exactly all their differences, and what the new jar is missing? If you dont know for sure (like you dont have access to release notes) then yes, you could find out than more dependencies jars were neededAutoionization
Thanks again. Your Answer did help me to perform the required debugging with the olden jars. I have added the exact Answer as a new Answer.Menfolk
S
2

The exception tells that the required class was not found in the classpath. As you have mentioned that you are adding PhantomJSDriver-jar as an external dependency. Make sure you have the correct scope for the jar and it is bundled when you package your application.

Refer to this question to get better understanding of the scope.

Steinberg answered 17/1, 2018 at 7:31 Comment(1)
Thanks @Steinberg Your answer was in the right direction.I have added the exact Answer as a new Answer.Menfolk
M
2

Finally the Question is Answered in a User Group by none other than Simon Stewart.

Answer : There's a version of phantomjsdriver ('com.codeborne:phantomjsdriver:jar:1.4.4') that appears to be kept up to date with latest selenium releases. I'd suggest using that.

Here is the snapshot of Simon's comment :

Simon_Stewart

Here is the working solution: PhantomJSDriver_solved

Menfolk answered 18/1, 2018 at 15:10 Comment(0)
P
1

even i had the same issue. try the below code. It worked for me;

    WebDriver driver;
    File src = new File("//PATH");
    System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setJavascriptEnabled(true);
    caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    driver = new PhantomJSDriver(caps);
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

    caps = new DesiredCapabilities();
    caps.setJavascriptEnabled(true);
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
            new String[] { "--web-security=no", "--ignore-ssl-errors=yes" });
    driver = new PhantomJSDriver(caps);

    driver.get("URL");
Patio answered 13/1, 2018 at 1:33 Comment(1)
Thanks for the Answer. My Question is not regarding the code but configurationMenfolk

© 2022 - 2024 — McMap. All rights reserved.