Thursday 28 May 2015

Read files from the root directory of your web application ( webapp/*) - Java

Was thinking about a good way to load internationalized error properties file to the web application, a simple resource bundle.
After a bit of thought, decided to make it simply this way:

Place it in the webapp/*

But how the hell do we read it from the java application which runs from my src/main/java?
Copy that at webapp/WEB_INF/i18n/*.properties and try reading the file at  ../../webapp/WEB-INF/i18n/<locale_name>/*.properties ?

Good luck!
Try that and let me know.

Life it not fair my friend and so are java programs on web apps!

Try this instead:
this.getClass().getClassLoader().getResourceAsStream("../i18n/<locale_name>/*.properties")

The classloader identifies the path starting from webapps\