在线程中出现异常:”main” java.lang.Error

huangapple go评论84阅读模式
英文:

Getting Exception in thread "main"java.lang.Error

问题

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SelIntroduction {

    public static void main(String[] args) {
        //调用浏览器
        //Chrome - ChromeDriver -> 方法
        //Chromedriver.exe -> 调用chrome浏览器//这是chrome提供的第三方库
        System.setProperty("webdriver.chrome.driver", "C:\\Users\888\\Downloads\\chromedriver_win32.exe");
        //webdriver.chrome.driver->路径的值
        WebDriver driver = new ChromeDriver();
    }
}

控制台:

在主线程中出现异常 "main" java.lang.Error: 未解析的编译问题: 无效的转义序列(有效的转义序列为 \b \t \n \f \r " ' \ )

Chrome浏览器应该会启动,但最终出现了异常。

英文:

CODE:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SelIntroduction {

    public static void main(String[] args) {
        //Invoking Browser
        //Chrome - ChromeDriver -> Methods
        //Chromedriver.exe -> invokes chrome browser//it is a third party library provided by chrome
        System.setProperty("webdriver.chrome.driver", "C:\Users\91888\Downloads\chromedriver_win32.exe");
        //webdriver.chrome.driver->value of the path 
        WebDriver driver = new ChromeDriver();
    }
}

CONSOLE:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Invalid escape sequence (valid ones are \b \t \n \f \r " ' \ )

at SelIntroduction.main(SelIntroduction.java:12)

Chrome browser should launch but ended up with an Exception

答案1

得分: 1

这不是一个异常,而是Eclipse编译器为您创建了一个类文件,以便您可以继续工作,尽管您的代码实际上无法在JDK中编译通过。就像消息中所说的那样,您在第12行有一个无效的转义序列。如果您要使用反斜杠指定Windows路径,那么反斜杠本身必须用额外的反斜杠进行转义。在您的源文件中,"C:\Users\91888\Downloads\chromedriver_win32.exe" 应该改为 "C:\\Users\\91888\\Downloads\\chromedriver_win32.exe",以便在生成的字符串中保留反斜杠。

英文:

This isn't an exception, this is the Eclipse compiler creating a class file for you so you can keep working even though your code wouldn't actually compile with the JDK. Like the message says, you have an invalid escape sequence on line 12. If you're going to specify Windows paths with the backslashes, the backslashes themselves have to be escaped--with additional backslashes. "C:\Users\91888\Downloads\chromedriver_win32.exe" should be "C:\\Users\\91888\\Downloads\\chromedriver_win32.exe" in your source file so that the backslashes are preserved in the resulting String.

答案2

得分: 0

错误信息...

在线程"main"中的异常java.lang.Error: 未解决的编译问题: 无效的转义序列(有效的转义序列为\b \t \n \f \r " ' \ )

...意味着在解释_ChromeDriver_绝对路径时存在编译错误。

在Java中提及系统路径时,您需要转义\字符以使编译器理解路径。


解决方案

您需要转义所有\字符,将所有\替换为\\。因此,代码行:

System.setProperty("webdriver.chrome.driver", "C:\Users888\Downloads\chromedriver_win32.exe");

需要更改为:

System.setProperty("webdriver.chrome.driver", "C:\\Users\888\\Downloads\\chromedriver_win32.exe");
英文:

This error message...

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Invalid escape sequence (valid ones are \b \t \n \f \r " ' \ )

...implies that there was a compilation error in the escape sequence while interpreting the ChromeDriver absolute path.

Using Java while mentioning the system paths you need to escape the \ character for the compiler to understand the path.


Solution

You need to escape all the \ characters, effectively replace all the \ with \\. So the line of code:

System.setProperty("webdriver.chrome.driver", "C:\Users888\Downloads\chromedriver_win32.exe");

needs to be:

System.setProperty("webdriver.chrome.driver", "C:\\Users\888\\Downloads\\chromedriver_win32.exe");

huangapple
  • 本文由 发表于 2023年6月16日 13:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定