Forums Register Login

How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?

+Pie Number of slices to send: Send
I have a webapp. I want to load a properties file, but I dont want to hardcode the full path of the properties file. I want to use relative paths under the webapp root. If I load a resource on the classpath such as under /WEB-INF/classes/app.properties, that works fine. But what if the properties file is under a different directory like /WEB-INF/configfiles/ ?

I also don't want to specify /WEB-INF/configfiles/ as part of the classpath when starting up the webapp. Is there a way to do what I want? Thanks.
+Pie Number of slices to send: Send
You can try having your config files outside WEB-INF. If you have your files in your-webapp/config folder - you can access the property file in your code as config/properties-file.
+Pie Number of slices to send: Send
 

Originally posted by Venkatraman Kandaswamy:
You can try having your config files outside WEB-INF. If you have your files in your-webapp/config folder - you can access the property file in your code as config/properties-file.


when the configuration file is stored under webapp, then it will be accessed easily by anyone who specifies the url to that file, Which is a security leak, if the configuration file has data base information or some other sensitive data.
+Pie Number of slices to send: Send
I am not sure of the benefits of having property file outside the classes folder, though it is secure & straightforward. However I am thinking of other ways that you can try (though I myself feel weird of my suggestions :roll: Again it is classpath - inside a jar file in /web-inf/lib. Otherwise keep it in web-inf/config folder and publish it through a dedicated & secured servlet other than your controller. If you are using framework like struts you can do it easily through an action path whose url pattern is protected from public. (have to try out and see)
If you could share the reason for keeping the property file outside classes folder, it might be useful for others.

[ February 15, 2008: Message edited by: Ray Johnson ]

[ February 15, 2008: Message edited by: Ray Johnson ]
[ February 15, 2008: Message edited by: Ray Johnson ]
+Pie Number of slices to send: Send
You can use context.getResource or context.getResourceAsStream, both of which take a context relative path for an argument.
+Pie Number of slices to send: Send
The reason is we have properties files and xml config files (lots actually) so we dont just want to dump them under the /WEB-INF/classes folder. So I tried putting them outside but then cant find them anymore in classpath. Dont want to use a startup parameter to add to the classpath either. Main reason to put it outside the /classes folder or /lib is for clarity of organization.
+Pie Number of slices to send: Send
I am also looking for some kind of solution to this problem. I want to keep my .properties files outside the web-app. I have created a new env variable and set the classpath in the server's Startup.sh batch file.
I called the .properties file by using the below code:

InputStream inputStream = getClass().getResourceAsStream("abc.properties");
logger.info("InputStream is: " + inputStream);

The value printed is 'null'.

Can anyone help?

Thanks.
Tanmoy
+Pie Number of slices to send: Send
How is it supposed to know where the file is? YOu need to provide the path to the file. Perhaps as a context parameter?

Classpath and environment variables will be of no help in your situation.
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 9827 times.
Similar Threads
web.xml and Struts 2
Accessing contents under WEB-INF directory
Properties file path hard coded
ClassLoader.getSystemResource
Lost properties
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:13:19.