File path in WebApplication using GlassFish

I am using a class to get the properties file in the source folder. But it won't work! After checking, I found that the default path using

File f = new File("/src/ss.properties");

      

is not the web application path, but the Glassfish configuration path! What if I want to get the properties file stored in the "classes" path? Typically, the default path is the path to the project.

I have used ClassLoader.getResourceAsStream("sss")

. But it returns null! I am sure the filename is correct because I tried it in another simple Java application.

Update : using

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

      

instead

ClassLoader.getSystemResource("sectionMapping.properties")

      

did the trick! I wonder why?

+2


source to share


1 answer


You must use getResourceAsStream

or similar. See this post for access to resources. (This is independent of Glassfish - it applies to all Java EE application servers.)

See also this JavaWorld article .

Update: If your file is in a location src/ss.properties

, make sure it was 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 the forward slash when using the method in java.lang.Class

)

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

      

Please note that you must use the full filename (including the extension .properties

).

If none of these functions work, replace the call getResourceAsStream

with getResource(...).openStream()

and post the details of the exception to be thrown.

+5


source







All Articles