Java 从jar读取资源是';他没有按预期工作

Java 从jar读取资源是';他没有按预期工作,java,selenium,jar,resources,packaging,Java,Selenium,Jar,Resources,Packaging,我正在尝试为selenium测试创建一个可执行jar。代码需要做的部分工作是设置一个系统属性,告诉Selenium在哪里可以找到驱动程序可执行文件(我正在使用chromedriver)。文件结构如下: src com mycompany SeleniumTest.java chromeDriver windows chromedriver.exe private static String WINDOWS_DRIVER

我正在尝试为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());
代码如下:

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)
线程“main”java.lang.IllegalStateException中的异常:驱动程序可执行文件 不存在:F:\temp\file:\F:\temp\seleniumTest.jar\chromeDriver\windows\chromeDriver.exe 位于com.google.common.base.premissions.checkState(premissions.java:177) 位于org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117) 位于org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112) 位于org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:75) 位于org.openqa.selenium.chrome.ChromeDriver(ChromeDriver.java:139) 然而,seleniumTest.jar存在于
F:\temp
中,错误消息指定的jar内路径也是如此


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

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

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


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

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

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


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

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

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


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

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

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

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

尝试以下方法:

// 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());
尝试以下方法:

// 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());
尝试以下方法:

// 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());
尝试以下方法:

// 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());


啊,谢谢,我不知道那个限制。我遵循的指导意见是“相反,我们可以使用Class.getResource获得路径,这将使我们能够在测试框架内捆绑Chrome驱动程序,即使它被捆绑到jar文件中。”这是错误的吗?我想是的。此外,如果它需要一个URL,而不是一个文件路径,它也不会工作,因为您正在对URL调用
getFile()
。啊,谢谢,我没有意识到这个限制。我遵循的指导意见是“相反,我们可以使用Class.getResource获得路径,这将使我们能够在测试框架内捆绑Chrome驱动程序,即使它被捆绑到jar文件中。”这是错误的吗?我想是的。此外,如果它需要一个URL,而不是一个文件路径,它也不会工作,因为您正在对URL调用
getFile()
。啊,谢谢,我没有意识到这个限制。我遵循的指导意见是“相反,我们可以使用Class.getResource获得路径,这将使我们能够在测试框架内捆绑Chrome驱动程序,即使它被捆绑到jar文件中。”这是错误的吗?我想是的。此外,如果它需要一个URL,而不是一个文件路径,它也不会工作,因为您正在对URL调用
getFile()
。啊,谢谢,我没有意识到这个限制。我遵循的指导意见是“相反,我们可以使用Class.getResource获得路径,这将使我们能够在测试框架内捆绑Chrome驱动程序,即使它被捆绑到jar文件中。”这是错误的吗?我想是的。此外,如果它需要一个URL,而不是一个文件路径,那么它也不会工作,因为您正在对URL调用
getFile()
。这与OP的问题中的代码基本相同。如果chrome驱动程序是indide the jar,它将不起作用。@SiKing-恐怕这对我不起作用。虽然它在Eclipse中执行时可以工作,但在jar中执行的代码会产生FileNotFoundException。@JBNizet你是对的,我是错的。不能在jar中执行文件,但可以将其作为文件进行操作。我已经编辑了我的代码。这是b