1

I have a Java application that uses SQLite as a Database. I want to execute some setup queries that are stored in a file called "setup.sql".

I call the following code to read the file:

BufferedReader reader = new BufferedReader(new FileReader(Resources.SETUP.file)))

Resources.SETUP.file is an enum that stores the file paths, in this case to "sql/setup.sql" located in the resources folder of my project.

I use this scheme for other files like images and localization, which works perfectly, like the following example for the logo image (Enum is "images/logo.png"):

Image logoImage = new Image(Resources.LOGO.file);

However with the sql file I always get a FileNotFoundException.

Am I using the Readers incorrectly?

The following does not work:

getClass().getResource(Resources.SETUP.file); -> InvocationTargetException

getClass().getClassLoader().getResource(Resources.SETUP.file); -> FileNotFoundException

Community
  • 1
  • 1
Christian Seiler
  • 1,130
  • 1
  • 13
  • 29
  • `getClass().getResource(Resources...))` – OneCricketeer Apr 16 '18 at 21:02
  • That does not work. Plus, please provide the duplicate you claim it shoud be. – Christian Seiler Apr 16 '18 at 21:09
  • Using getClassLoader returns the correct url, however I still have a FileNotFoundException – Christian Seiler Apr 16 '18 at 21:30
  • Are you using Maven to package and run your code? Is the file being copied into your classpath? I.e. When you run the code, the `src/main/resources` folder isn't what's being read from – OneCricketeer Apr 16 '18 at 22:00
  • If I remeber correctly `new Image()` takes resource from class path, while `FileReader()` expects a path in file system, either absolute or relative to directory from where JVM started. So if you use maven and run it in IDE path should be `src/main/resources/sql/setup.sql`. Still remember it is relative to directory from where `java` command called. – Vadim Apr 16 '18 at 22:32
  • The FileNotFoundException shows the path /Users/.../.../target/classes/sql/setup.sql which is where that file is (after compilation) – Christian Seiler Apr 17 '18 at 05:01
  • The file is also in the jar archive at the correct location. And yes I use maven. – Christian Seiler Apr 17 '18 at 05:05

0 Answers0