KunLun
KunLun

Reputation: 3227

Java System.setProperty add project path in front of my path

I want to set as system property a file path from resources folder.

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:\JavaIDEA\projname\file:\D:\JavaIDEA\projname\target\classes\chromedriver-76.0.3809.68.exe

Why when I setProperty, it add in front of my path the path of project?

I also tried this: path = path.replace("/", "\\\\"); - same result

I'm using Windows.

Upvotes: 1

Views: 2534

Answers (1)

Kris
Kris

Reputation: 8853

Try

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

Upvotes: 2

Related Questions