1

I am working on a Maven project and I would like to access the resources folder that contains an executable: geckodriver.exe

Path: src/main/resources/geckodriver/

I know after packaging, the is copied in target. Therefore, the file should be accessible via classpath. Then I should be able to access the file by writing this: MyClass.class.getClassLoader().getResource("geckodriver/geckodriver.exe").

An exception is displayed when I run the project: java.lang.IllegalStateException: The driver executable does not exist

Is there a way to fix this ?

lharry
  • 135
  • 1
  • 3
  • 16
  • You can't use `getResource` if you packaged to a jar file and runs from that, because the file is inside the jar file, and not on the file system. Is that what you did? – Andreas Mar 16 '19 at 21:30
  • The package is a `jar`. The idea is to have an executable jar that contains all the files necessary to run. – lharry Mar 16 '19 at 21:36
  • 3
    You need to copy the resource(s) to some location on the harddisk, e.g. to a temporary directory: [How to create a temporary directory/folder in Java?](https://stackoverflow.com/q/617414/5221149). Remember to clean up (delete the folder and files) when done. Copy should use [`getResourceAsStream()`](https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResourceAsStream-java.lang.String-) and [`Files.copy(InputStream in, Path target)`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-) – Andreas Mar 16 '19 at 21:46
  • @Andreas ^^^That^^^ was in short what OP needs to do exactly. – undetected Selenium Mar 16 '19 at 22:01

0 Answers0