Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动JAR应用程序未从资源文件夹读取chromedriver.exe_Java_Spring_Spring Boot_Selenium - Fatal编程技术网

Java Spring启动JAR应用程序未从资源文件夹读取chromedriver.exe

Java Spring启动JAR应用程序未从资源文件夹读取chromedriver.exe,java,spring,spring-boot,selenium,Java,Spring,Spring Boot,Selenium,我有一个SpringBoot项目,它使用selenium对不同的应用程序进行自动化测试。项目的输出归档文件是一个JAR文件。我有下面的代码来启动chrome浏览器 static { try { Resource resource = null; String osName = System.getProperty("os.name").toLowerCase(); if (osName.contains(IAutomationConstan

我有一个SpringBoot项目,它使用selenium对不同的应用程序进行自动化测试。项目的输出归档文件是一个JAR文件。我有下面的代码来启动chrome浏览器

static {
    try {
        Resource resource = null;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains(IAutomationConstans.WINDOWS_OS_NAME)) {
            resource = new ClassPathResource("chromedriver.exe");
        } else if (osName.contains(IAutomationConstans.LINUX_OS_NAME)) {
            resource = new ClassPathResource("chromedriver");
        }
        System.setProperty("webdriver.chrome.driver", resource.getFile().getPath());
        ChromeOptions capabilities = new ChromeOptions();
        webdriver = new ChromeDriver(capabilities);
        Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
    } catch (Exception e) {
        System.out.println("Not able to load Chrome web browser "+e.getMessage());
    }
}
除此之外,我还有下面执行自动化代码的Spring引导代码

@SpringBootApplication
@ComponentScan("com.test.automation")
@PropertySource(ignoreResourceNotFound = false, value = "classpath:application.properties")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, 
DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestAutomation{

public static void main(String[] args) {
    System.out.println("&&&&&&&&&&&&&&&&&&&&&");
    SpringApplication.run(TestAutomation.class, args);
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%");
}
}
我已将chromedriver.exe放在src\main\resources下。若我通过右键单击从eclipse执行TestAutomation类,那个么一切正常

但若我通过mvn包命令生成jar文件并执行jar文件,那个么我将得到以下错误

Not able to load Chrome web browser class path resource [chromedriver.exe] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/user/automation/target/automationapp-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/chromedriver.exe

尝试使用ResourceLoader

@Autowired
ResourceLoader resourceLoader;

...
Resource resource = resourceLoader.getResource("classpath:chromedriver.exe")
...
getFile()希望资源本身在文件系统上可用,即它不能嵌套在jar文件中。在这种情况下,resource.getInputStream()将起作用。您需要修改代码,因为这行代码
System.setProperty(“webdriver.chrome.driver”,resource.getFile().getPath())将不起作用

请参阅以下内容: