0

I am having some trouble getting this code to work. Please keep in mind that I am very new to this.

package mypackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class myclass {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\iftikhar\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://newtours.demoaut.com";
        driver.get(baseUrl);
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = driver.getTitle();

        if (actualTitle.equals(expectedTitle)) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }

        driver.close();
    }
}

The error I am getting is:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at mypackage.myclass.main(myclass.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    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)
    ... 1 more

Can someone please explain this error to me and help me out?

Thanks

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Do you have geckodriver.exe installed in your specific path? – Vishnu T S Dec 20 '17 at 02:44
  • 1
    Don't know, but my guess would be that your imports are failing. Maybe for a test, just have a basic bit of code that imports some libraries that you are absolutely certain exist. I'm particularly suspicious of the FireFox import. – ouflak Dec 20 '17 at 02:46
  • 1
    how do you manage your dependencies? – Eugene S Dec 20 '17 at 02:50
  • Check this Discussion [Exception in thread “main” java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver](https://stackoverflow.com/questions/47823506/exception-in-thread-main-java-lang-noclassdeffounderror-org-openqa-selenium-w/47845104#47845104) – undetected Selenium Dec 20 '17 at 05:29

2 Answers2

0

This looks like a problem with Guava libraries. If you use maven to manage your dependencies, set a Guava version manually like this:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>
Eugene S
  • 6,709
  • 8
  • 57
  • 91
0

The error stack trace says it all :

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at mypackage.myclass.main(myclass.java:11)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function

First of all ClassNotFoundException was thrown which inturn raised NoClassDefFoundError


When does ClassNotFoundException occurs :

java.lang.classNotFoundException occurs in following cases:

  • When we try to load a class by using Class.methodName() method and .class file or binary of class is not available in classpath.
  • When Classloader try to load a class by using findSystemClass() method.
  • While using loadClass() method of class ClassLoader in Java.
  • But majority of the time, ClassNotFoundException will come only when JVM tries to load a class at run-time.

What is ClassNotFoundException

ClassNotFoundException in Java is a subclass of java.lang.Exception and occurs when JVM tries to load a particular class and doesn't finds the requested class in classpath. It is a Checked Exception and you need to provide Exception Handling explicitly while using methods which can possibly throw ClassNotFoundException either by using try-catch block or by using throws clause.

What went wrong :

The problem statement is :

if (actualTitle.equals(expectedTitle))

If you look at the documentation of equals() method, it is defined as :

equals

public boolean equals(Object anObject)

  Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides: 
    equals in class Object 

Parameters: 
    anObject - The object to compare this String against 

Returns: 
    true if the given object represents a String equivalent to this string, false otherwise 

Explaination :

In your code block you have invoked equals() on the String actualTitle which expects an Object as an argument. But you have provided expectedTitle as an argument which is of type String instead of an an Object which was defined as follows :

String expectedTitle = "Welcome: Mercury Tours";

Solution:

The simple solution would be to use either of the following methods :

  • equalsIgnoreCase : public boolean equalsIgnoreCase(String anotherString)
  • contains : public boolean contains(CharSequence s)
  • contentEquals : public boolean contentEquals(StringBuffer sb)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352