Java 如何在 JAR 中包含 ChromeDriver?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21650178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I include ChromeDriver in a JAR?
提问by DaniChan
I'm writing a web-automation program and so far, it works fine using Selenium's FirefoxDriver. However, I want to make it use Chrome if Firefox is not installed. I downloaded the ChromeDriver, put it inside a folder in my Eclipse project, and ran it. After I added System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
it worked fine. However, when I try to export it (using Eclipse's standard Export). It crashes, I believe because it can't find the ChromeDriver (I think this because exporting it with FirefoxDriver works fine).
我正在编写一个网络自动化程序,到目前为止,它使用 Selenium 的 FirefoxDriver 运行良好。但是,如果未安装 Firefox,我想让它使用 Chrome。我下载了 ChromeDriver,把它放在我的 Eclipse 项目的一个文件夹中,然后运行它。我添加后System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
它工作正常。但是,当我尝试导出它时(使用 Eclipse 的标准导出)。它崩溃了,我相信是因为它找不到 ChromeDriver(我认为这是因为使用 FirefoxDriver 导出它可以正常工作)。
I have tried changing the .JAR to a .ZIP in order to look inside, and I see that the driver
folder was stripped away, simply putting chromedriver.exe
inside the top-level JAR. I tried changing the property to System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
but still crashes.
我尝试将 .JAR 更改为 .ZIP 以查看内部,我看到该driver
文件夹已被剥离,只需放入chromedriver.exe
顶级 JAR。我尝试将属性更改为System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
但仍然崩溃。
Does anyone know why the exported JAR is unable to find chromeDriver
, despite the fact that I have verified that it is in the JAR?
有谁知道为什么导出的 JAR 无法找到chromeDriver
,尽管我已经验证它在 JAR 中?
Thanks, Daniel
谢谢,丹尼尔
采纳答案by Ittiel
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
它在 Jar 中,但无法执行(与尝试从 zip 文件运行 .exe 相同),您必须在运行之前将其解压缩
回答by Sighil
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.
将您的代码导出为没有 chromedriver 的 jar。创建一个文件夹 ChromeDriver。将您的 chromedriver.exe 放在此文件夹中。将 ChromeDriver 文件夹与您的 jar 放在一起。
Also dont forget to set the System property in the code to
另外不要忘记将代码中的 System 属性设置为
System.setProperty("webdriver.chrome.driver", "ChromeDriver/chromedriver.exe");
Please let me know if this works for you.
请让我知道这是否适合您。
回答by user1565473
you have to extract it before running it. Copy Your driver under src/resources/drivers/chromedriver.exe
你必须在运行它之前提取它。在 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.
现在,无论何时运行此代码,它都会创建一个新文件夹“驱动程序”,您可以在其中放置 Runnable jar 并复制您可以访问的驱动程序。
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();