I'm attempting to create an executable jar for a selenium test. Part of the things the code needs to do is set a system property to tell Selenium where the driver executable can be found (I'm using the chromedriver). File structure is as follows:
src
com
mycompany
SeleniumTest.java
chromeDriver
windows
chromedriver.exe
And the code is as follows:
private static String WINDOWS_DRIVER = "/chromeDriver/windows/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
SeleniumTest.class.getResource(WINDOWS_DRIVER).getFile());
When executed in eclipse, this code works fine. However, when I export to a runnable jar file (from eclipse) I get the following error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable
does not exist: F:\temp\file:\F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:75)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:139)
And yet seleniumTest.jar exists at F:\temp
as does the path within the jar which the error message specifies.
Any ideas on what is wrong or suggestions to try? I've tried changing the slahses to backslashes and also (just as a test) hard coding the path (e.g. setting the system property to F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe
), but neither has worked.
The system property is supposed to contain the path to the file, on the file system, where the driver can be found and executed.
The driver is not a file. It's an entry of your jar file. Executables bundled in a jar file can't be executed.
If you really want to bundle the driver into your jar file and execute it, then you'll have to read the bytes from this classpath resource, write them to a temporary executable file, and then tell selenium where this temporary executable file is located.