2

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

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: Using

this.getClass().getClassLoader().getResourceAsStream("sectionMapping.properties");

instead of

ClassLoader.getSystemResource("sectionMapping.properties")

did the trick! I wonder why?

exhuma
  • 20,071
  • 12
  • 90
  • 123
Sefler
  • 2,237
  • 5
  • 19
  • 29

1 Answers1

5

You should use getResourceAsStream, or similar. See this post for how to access resources. (This is independent of Glassfish - it applies to all Java EE 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 method 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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191