0

I'm trying to use a local file in which I've specified my db connection properties which is named dao.properties. And I'm proceeding this way:

InputStream fichierProperties = classLoader.getResourceAsStream( "/src/dao/dao.properties" );

However, when using this path, I'm getting an exception stating that the debugger wasn't able to find that file.

Here are some packages in my project:

enter image description here

The dao.properties is just under the dao package.

How do I resolve this, please?

Radix
  • 143
  • 2
  • 9
Lucie kulza
  • 1,357
  • 6
  • 20
  • 31
  • Which Classloader are you using? What build tool are you using? Is it a Webapp? – Mikkel Løkke May 26 '14 at 14:02
  • The `resources` are to be found in the classpath. So a) it will not be inside the `src` directory and b) you only need to specify the package path. So, "dao/dao.properties" – SJuan76 May 26 '14 at 14:09
  • @SJuan76, that've worked! would you make an answer ? – Lucie kulza May 26 '14 at 14:11
  • 1
    I guess @SJuan76 that is you know how things works, so why you bother to down vote? This is a place to ask, not to critic. Consider add useful information please. – Victor May 26 '14 at 14:52
  • this post explain how the classLoader works [here][1] [1]: http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – oussama.elhadri May 26 '14 at 15:44

1 Answers1

2

If you put the file inside the src folder, the IDE probably is packaging, when instructed to compile and build, the file into the bundled generated jar. So you can reach with the method GetResourceAsStream.

So if you put the file (dao.properties) in root folder of your sources files (generally the src folder), just simple referring to dao.properties will refer to the resource.

If you put the file inside a subfolder of src, the correct way to reference it would be subfolder/dao.properties.

The first "/" is not necessary as the getResourceAsStream always search in the classpath, that for default is the root of the sources folder, inside the jar. (where are not talking about external files!)

Updated:

Assuming you place a file name notes.txt inside a folder(package) named ´sub´, this is valid example, only for purporses of how to get a bundled file that is in jar.

public class Main {

public static void main (String[] args) throws IOException {
    InputStream is = Main.class.getResourceAsStream("sub/notes.txt");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    while (s != null) {
        System.out.println (s);
        s = br.readLine();
    }
    is.close();
}
}

I add more information about this, by referring to this post

miken32
  • 42,008
  • 16
  • 111
  • 154
Victor
  • 3,841
  • 2
  • 37
  • 63
  • Why the downvote? Consider please add somethign useful... i really don't understand what is the point to donwvote without comments. Is totally void. – Victor May 26 '14 at 14:50