6

I currently have a project in Java set up with the following directory structure in Eclipse:

enter image description here

And in my code I have the following lines:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("resources/config");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

However, the InputStream is always gets assigned to null, which causes a crash when it gets to the second line. I know it has something to do with how I set up the path that it's looking for, but I can't figure out exactly why it isn't working.

Rick-777
  • 9,714
  • 5
  • 34
  • 50
Giardino
  • 1,367
  • 3
  • 10
  • 30
  • Make `resources` a source folder with Eclipse. – Sotirios Delimanolis Aug 22 '13 at 16:59
  • When I export the finished code as an executable jar, will that cause issues with your suggestion? I basically want to be able to edit this config even after I have exported it as an executable. – Giardino Aug 22 '13 at 17:00
  • 1
    @SotiriosDelimanolis then use `InputStream is = this.getClass().getClassLoader().getResourceAsStream("/config");` :) – Katona Aug 22 '13 at 17:00
  • @Katona I set resouces to be a src folder for the project and changed the line you specified (now its just looking for "/config") and it still throws null pointers when it gets to the bufferedreader line =/ – Giardino Aug 22 '13 at 17:11

2 Answers2

9

Your config file is in your project, somewhere on the file system.

However, Eclipse isn't putting it on the classpath. To force it to be on the classpath, right click your folder and add it as a source folder. Eclipse will then add it to the root of the classpath. You can retrieve it with

InputStream is = this.getClass().getResourceAsStream("/config");

Eclipse puts everything in resources source folder starting at the root of the classpath. Therefore

resources/config

will appear in classpath as

/config
/qbooksprintfix/FileChecker
/qbooksprintfxi/FilePurgeHandler
/...
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • So I can't have src AND resources as source folders? Separating them would be ideal so I can edit the config easier once I export it. – Giardino Aug 22 '13 at 17:07
  • @user1806716 You can have them both (and more) as source folders. If you don't want your file to be at root, you can make a second `resources` folder inside the original `resources` folder as `/resources/resources/config`. The file would then be accessible as `getResourceAsStream("/resources/config")`. You still have to make the original one a `source` folder though on Eclipse's Build path. You might want to look into Maven as it does something similar. – Sotirios Delimanolis Aug 22 '13 at 17:09
  • I set resouces to be a src folder for the project and changed the line you specified (now its just looking for "/config") and it still throws null pointers when it gets to the bufferedreader line =/ – Giardino Aug 22 '13 at 17:12
  • yeah, just tried it, it is `this.getClass().getClassLoader().getResourceAsStream("config");` correctly (no leading `/`) or `this.getClass().getResourceAsStream("/config");` which searches relative to the class so it needs the leading `/` – Katona Aug 22 '13 at 17:13
  • @Katona That's just a coincidence with the `ClassLoader`, you should specify absolute paths. – Sotirios Delimanolis Aug 22 '13 at 17:13
  • @user1806716 Go into your Project properties and look at the Deployment Assembly. Is your `resources` source folder going to `/`? – Sotirios Delimanolis Aug 22 '13 at 17:14
0

Try whit InputStream is = this.getClass().getClassLoader().getResource("/resources/config").openStream();

or InputStream is = this.getClass().getClassLoader().getResourceAsStream("/resources/config");

In both cases make sure to put "/" before the "resources"

Abstract
  • 664
  • 1
  • 5
  • 15