How can I include ChromeDriver in a JAR?
you have to extract it before running it.Copy Your driver under src/resources/drivers/chromedriver.exe
Now whenever you run this code , it will Create a new Folder "Driver" where ever you put your Runnable jar and copy your driver, which you can access.
ClassLoader classLoader = getClass().getClassLoader(); URL resource = classLoader.getResource("resources/drivers/chromedriver.exe"); File f = new File("Driver"); if (!f.exists()) { f.mkdirs(); } File chromeDriver = new File("Driver" + File.separator + "chromedriver.exe"); if (!chromeDriver.exists()) { chromeDriver.createNewFile(); org.apache.commons.io.FileUtils.copyURLToFile(resource, chromeDriver); } System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath()); driver = new ChromeDriver();
Export your code as jar without the chromedriver.Create a folder ChromeDriver. Place your chromedriver.exe in this folder.Place ChromeDriver folder along with your jar.
Also dont forget to set the System property in the code to
System.setProperty("webdriver.chrome.driver", "ChromeDriver/chromedriver.exe");
Please let me know if this works for you.
It's in the Jar but cannot be executed (same as trying to run an .exe from a zip file), you have to extract it before running it