Java System.SetProperty在我的路径前添加项目路径



我想设置为系统属性,从资源文件夹中的文件路径。

String path = MainCore.class.getClassLoader().getResource("chromedriver-76.0.3809.68.exe").toExternalForm();
System.out.println(path);
//file:/D:/JavaIDEA/projname/target/classes/chromedriver-76.0.3809.68.exe
System.setProperty("webdriver.chrome.driver", path);
//IllegalStateException: The driver executable does not exist: 
//D:JavaIDEAprojnamefile:D:JavaIDEAprojnametargetclasseschromedriver-76.0.3809.68.exe

为什么当我 setProperty时,它在我的路径前面添加了项目路径?

我也尝试过:path = path.replace("/", "\\");-相同结果

我正在使用Windows。

尝试

String absolutePath = new File(MainCore.class.getClassLoader().getResource("chromedriver-76.0.3809.68.exe").getFile()).getAbsolutePath();
System.setProperty("webdriver.chrome.driver", path);

最新更新