我在使用此代码时遇到一些麻烦。请记住,我对此很新。
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();
}
}
我得到的错误是:
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
有人可以向我解释这个错误并帮助我吗?
谢谢
这看起来像是Guava库的一个问题。如果您使用maven来管理依赖项,请手动设置Guava版本,如下所示:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
错误堆栈跟踪说明了一切:
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
首先,qazxsw poi被抛出,反过来提升了qazxsw poi
ClassNotFoundException
何时发生:NoClassDefFoundError
发生在以下情况:
ClassNotFoundException
方法加载一个类时,java.lang.classNotFoundException
文件或类的二进制文件在类路径中不可用。Class.methodName()
尝试使用.class
方法加载一个类。Classloader
的findSystemClass()
方法。loadClass()
只有在JVM尝试在运行时加载一个类时才会出现。ClassLoader
Java中的ClassNotFoundException
是ClassNotFoundException
的子类,当ClassNotFoundException
尝试加载特定类并且在类路径中找不到请求的类时发生。它是一个java.lang.Exception
,你需要明确地提供JVM
,同时使用可能通过使用Checked Exception
块或使用Exception Handling
子句抛出ClassNotFoundException
的方法。
问题陈述是:
try-catch
如果你看一下throws
方法的文档,它被定义为:
if (actualTitle.equals(expectedTitle))
在你的代码块中,你在equals()
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
上调用了equals()
,它需要一个Object作为参数。但是您提供了String
作为参数,其类型为String而不是Object,其定义如下:
actualTitle
简单的解决方案是使用以下任一方法:
expectedTitle
:public boolean equalsIgnoreCase(String anotherString)String expectedTitle = "Welcome: Mercury Tours";
:public boolean contains(CharSequence s)equalsIgnoreCase
:public boolean contentEquals(StringBuffer sb)