tom44
tom44

Reputation: 63

chromedriver not instantiating in Java

I am using the following code to try to create a Java program (in Eclipse Neon) that uses Selenium. I have tried a whole bunch of different combinations of backslashes and forwards slashes like "C://Users/talai/..." and "C:\Users\talai" etc. but nothing is working.

Below is my code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import java.awt.Dimension;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;

public class Run {

    public static void main(String[] args) throws InterruptedException {
    for(int d=0; d<20; d++)//run program d<___ times
    {
        //One "Play" session = 5m 15s
        //One "Collect" session = ???
        System.out.println("Session " +d+ " of 300 starting...");
        System.setProperty("webdriver.chrome.driver", "C://Users/talai/Desktop/code/ChipCollector/chromedriver.exe");
        WebDriver driver = new ChromeDriver();

And this is the output:

Session 0 of 300 starting...
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at myVegas.Run.main(Run.java:44)
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

Upvotes: 0

Views: 402

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193098

A few words :

  • As per best practices you shouldn't open 20 ChromeDriver and Chrome Browser instances at a time without any proper usecase and availability of proper hardware. So you need to remove the for() loop as in :

    for(int d=0; d<20; d++)
    
  • System Paths in Java are expressed either through double back slashes (\\) or single forward slash (/). Both works identically.

  • The error you are seeing says it all :

     Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
         at myVegas.Run.main(Run.java:44)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    

The error implies that java.lang.NoClassDefFoundError was raised which was caused by java.lang.ClassNotFoundException.


What is NoClassDefFoundError

NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.

This clearly indicates that Selenium is trying to resolve the ChromeDriver class at runtime from import org.openqa.selenium.chrome.ChromeDriver; which is no more available.


What went wrong :

From the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.

This situation occurs if there are presence of multiple sources to resolve the Classes and Methods as :

  • Presence of selenium-server-standalone-3.9.1 JAR and JARs from selenium-java-3.9.1 within your Project Space.
  • Presence of selenium-server-standalone-3.9.1 JAR and Maven related JARs.
  • Presence of selenium-server-standalone-3.9.1 JAR and Gradle related JARs.

Solution :

Here are a few steps to solve NoClassDefFoundError :

  • Use either selenium-server-standalone-3.9.1 JAR or JARs from selenium-java-3.9.1
  • If using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
  • While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
  • Remove the unwanted other <dependency> from pom.xml
  • Clean you Project Workspace within your IDE regularly only to build your project with required dependencies.
  • Use CCleaner tool to wipe away the OS chores periodically.
  • While you execute a Maven Project always do maven clean, maven install and then maven test
  • If your Web Browser base version is too old uninstall the Web Browser through Revo Uninstaller and install a recent GA released version of the Web Browser.

Upvotes: 1

anurag0510
anurag0510

Reputation: 763

Your JRE can't find a class. It can't find the class com.google.common.base.Function, because you didn't added it to class path.

Just add appropriate Selenium Standalone as per suited version by you in your project class path simple. It should do it.

Hope it helped.

Upvotes: 0

Related Questions