0

I have standard maven project structure:

-src
 |
 --main
   | 
   ---java
   ---resources
      |
      ----file.txt

I need to read this file on app start (read DB schema and execute SQL script, so this file is opened via FileReader). When app is built, then using the following code gets it from project/target/classes/file.txt:

getClass().getResource("/file.txt").getFile()

This file also appears in the root of the result jar file. And after app start it cannot be accessed because it is searched near the jar file not inside.

How to access this file when it is inside jar/resource folder?

Dragon
  • 2,431
  • 11
  • 42
  • 68

1 Answers1

1

What you need is to read the file as a resource, not as a file with

InputStream stream = getClass().getClassLoader().getResourceAsStream("file.txt");

Relevant question and answers can be found here:

How to really read text file from classpath in Java

Community
  • 1
  • 1
harun
  • 1,889
  • 18
  • 19