tags:

views:

521

answers:

1

I'm using a class to get a property file under the source folder. But it doesn't work! After checking, I found that the default path by using the File f = new File("/src/ss.properties"); is not the web application path but the glassfish config path! What can I do if I want to fetch the property file stored in the "classes" path? Usually the default path is the project path, you know.


I've used the ClassLoader.getResourceAsStream("sss") .But it returns null! I'm sure the file name is correct because I've tried it in another simple JAVA app.


Update: Hi~ I used this.getClass().getClassLoader().getResourceAsStream("sectionMapping.properties"); instead of ClassLoader.getSystemResource("sectionMapping.properties") did the trick! I wonder why?

+2  A: 

You should use getResourceAsStream, or similar. See this post for how to access resources. (This is independent of Glassfish - it applies to all JEE app servers.)

See also this JavaWorld article.

Update: If your file is in the location src/ss.properties, check that it has been copied to WEB-INF/classes. Then, you should be able to access it with the following code:

InputStream propStream = ClassLoader.getResourceAsStream("ss.properties");

or (note leading slash if using the methid in java.lang.Class)

InputStream propStream = Class.getResourceAsStream("/ss.properties");

Note that the full file name (including the .properties extension) needs to be used.

If neither of these work, please replace the getResourceAsStream call with getResource(...).openStream() and post details of the exception which should be thrown.

Vinay Sajip