1

Hello: I have not found anything useful on the web, so I ask the question. How can I read files from the resource folder? I have a source folder (src) and a resources folder (res). Both are specified in Java as a source folder. I tried to use: new File(Main.class.getClassLoader().getResource("shader.glsl").getFile()); but it didn't work. Please do not be too strict just ask if I forgot something to mention.

These are the errors that I get:

java.io.FileNotFoundException: shader.glsl (Das System kann die angegebene Datei nicht finden)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at com.leocat.game.shader.ShaderProgram.loadShader(ShaderProgram.java:54)
    at com.leocat.game.shader.ShaderProgram.init(ShaderProgram.java:18)
    at com.leocat.game.shader.StaticShader.init(StaticShader.java:8)
    at com.leocat.game.states.GameState.init(GameState.java:29)
    at com.leocat.game.Game.init(Game.java:91)
    at com.leocat.game.Game.updateGLFW(Game.java:60)
    at com.leocat.game.Game.run(Game.java:132)
    at java.lang.Thread.run(Unknown Source)

Thanks in advance!

Leocat
  • 109
  • 1
  • 1
  • 11

4 Answers4

7

Dont use File API to get resource files because it may work eg during development in IDE (because all compiled filesa are placed on disc) and will not work after build and deployment of JAR (because JAR content is not accessible to native file system)

Instead of getting file, get resource stream directly. With file API you would have to get FileInputStream at some point anyway. So to make it work everywhere use

InputStream in = Main.class.getClassLoader().getResourceAsStream("shader.glsl");

In this case, shader.glsl must be present in resource folder (if you are using maven) or to be more precise and universal, it must be compiled into root of JAR file. If it is nested in JAR you will have to use /path/to/resource/shader.glsl to get that resource

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
2

If you have shader.glsl file in your resource folder. I.e. when you build jar file it will copy to the jar root. Then you can read it as stream:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream in = classloader.getResourceAsStream("/shader.glsl");
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
1

For accessing a resource from the classpath

new ClassPathResource("data/employees.dat", this.getClass().getClassLoader());

new ClassPathResource("../../../data/employees.dat", Example.class).getFile();

retrieve your resource with getResource:

resourceLoader.getResource("classpath:data/employees.dat");

By ApplicationContext

context.getResource("classpath:data/employees.dat");

Reading as an InputStream

InputStream resource = new ClassPathResource("data/employees.dat").getInputStream();
harkesh kumar
  • 833
  • 2
  • 13
  • 35
1

You may use Guava's Resources class to safely access resources. It has convenience methods to reading them as String or List<String> of lines. Also it will fail with more meaningful exceptions in case of misconfiguration.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103