简体   繁体   English

如何获取驻留在src / main / resources中的.exe文件的路径

[英]How to get the path for .exe file which resides in src/main/resources

I am doing automation for one of our project, for that I coded like the below: 我正在为我们的项目之一进行自动化,为此我编写了如下代码:

public void m() throws FileNotFoundException, IOException {
        System.setProperty("webdriver.chrome.driver",Thread.currentThread().getContextClassLoader().getResource("chromedriver.exe").getFile());
        System.out.println("123");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
    }

It's working when I execute in eclipse. 当我在Eclipse中执行时,它正在工作。 but when I am testing with the maven generated jar, it is giving an exception : 但是当我用Maven生成的jar测试时,它给出了一个例外:

Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: C:\\Users\\rkowsu\\Desktop\\jar\\file:\\C:\\Users\\UU1\\Desktop\\jar\\resources-part-0.0.1-SNAPSHOT.jar!\\chromedriver.exe 线程“主”中的异常java.lang.IllegalStateException:驱动程序可执行文件不存在: C:\\Users\\rkowsu\\Desktop\\jar\\file:\\C:\\Users\\UU1\\Desktop\\jar\\resources-part-0.0.1-SNAPSHOT.jar!\\chromedriver.exe

Is there anything wrong? 有什么问题吗?

If you have chromedriver.exe in the src/main/resources directory of your application then it will get packaged up into your jar file. 如果您的应用程序的src/main/resources目录中有chromedriver.exe,则它将打包到您的jar文件中。

It can't be executed from there. 它不能从那里执行。

Have a look at the answer to How to work with chrome driver in Maven . 看看如何在Maven中使用chrome驱动程序的答案。

For System.setProperty() you should be providing it with "webdriver.chrome.driver" and the relative\\path\\to\\exe . 对于System.setProperty() ,应为其提供"webdriver.chrome.driver"relative\\path\\to\\exe If your project structure looks like this: 如果您的项目结构如下所示:

src
    main
          java
             App.java
          resources
             chromedriver.exe

You should use System.setProperty("webdriver.chrome.driver", "src" + File.separator + "main" + File.separator + "resources" + File.separator +"chromedriver.exe") 您应该使用System.setProperty("webdriver.chrome.driver", "src" + File.separator + "main" + File.separator + "resources" + File.separator +"chromedriver.exe")

This should be able to find the .exe whether or not you run from a jar or not. 无论您是否从jar运行,这都应该能够找到.exe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM