Reputation: 63
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
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.
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.
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 :
Here are a few steps to solve NoClassDefFoundError
:
<artifactId>selenium-java</artifactId>
or <artifactId>selenium-server</artifactId>
. Avoid using both at the same time.<dependency>
from pom.xml
maven clean
, maven install
and then maven test
Upvotes: 1
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