【问题标题】:Reading resource from jar isn't working as expected从 jar 读取资源未按预期工作
【发布时间】:2014-07-20 13:58:58
【问题描述】:

我正在尝试为 selenium 测试创建一个可执行 jar。代码需要做的部分事情是设置一个系统属性来告诉 Selenium 在哪里可以找到驱动程序可执行文件(我正在使用 chromedriver)。文件结构如下:

src
   com
      mycompany
           SeleniumTest.java
   chromeDriver
      windows
         chromedriver.exe

代码如下:

private static String WINDOWS_DRIVER = "/chromeDriver/windows/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
                    SeleniumTest.class.getResource(WINDOWS_DRIVER).getFile());

在 Eclipse 中执行时,此代码运行良好。但是,当我导出到可运行的 jar 文件(来自 eclipse)时,出现以下错误:

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)

然而 seleniumTest.jar 存在于 F:\temp 中,错误消息指定的 jar 中的路径也是如此。

关于什么是错误的任何想法或尝试的建议?我尝试将斜杠更改为反斜杠,并且(仅作为测试)对路径进行硬编码(例如将系统属性设置为 F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe),但都没有奏效。

【问题讨论】:

    标签: java selenium jar resources packaging


    【解决方案1】:

    系统属性应该包含文件的路径,在文件系统上可以找到并执行驱动程序。

    驱动程序不是文件。这是您的 jar 文件的一个条目。无法执行捆绑在 jar 文件中的可执行文件。

    如果你真的想将驱动程序捆绑到你的 jar 文件中并执行它,那么你必须从这个类路径资源中读取字节,将它们写入一个临时可执行文件,然后告诉 selenium 这个临时可执行文件在哪里位于。

    【讨论】:

    • 啊,谢谢,我不知道这个限制。我正在关注this post 寻求指导,建议“而是通过使用 Class.getResource 来获取路径,这将使我们能够将 Chrome 驱动程序捆绑到我们的测试框架中,即使它被捆绑到一个 jar 文件中。” i> 这是错的吗?
    • 我想是的,是的。此外,如果它需要一个 URL,而不是文件路径,它也不会工作,因为你在 URL 上调用 getFile()
    【解决方案2】:

    尝试类似:

    // locate chromedriver in the jar resources
    URL res = getClass().getResource("/chromeDriver/windows/chromedriver.exe");
    // locate chromedriver in the jar filesystem
    File f = new File(res.getFile());
    // copy chromedriver out into the real filesystem
    File target = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + f.getName());
    java.nio.file.Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
    if (!target.canExecute())
        throw new FileNotFoundException("chrome.exe copy did not work!");
    System.setProperty("webdriver.chrome.driver", target.getCanonicalPath());
    

    【讨论】:

    • 这与 OP 的问题中的代码基本相同。如果 chrome 驱动程序在 jar 中,它将无法工作。
    • @SiKing - 恐怕这对我不起作用。虽然它在 Eclipse 中执行时可以工作,但从 jar 中执行时的代码会给出 FileNotFoundException。
    • @JBNizet 你是对的,我错了。您不能在 jar 中执行文件,但可以将它们作为文件进行操作。我已经编辑了我的代码。
    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多