views:

44

answers:

2

Hi,

I have a JEE 6 application with JSF 2 and Tomcat 7. Now I have a POJO. This POJO should read a properties file. The properties file is is located in WEB-INF/classes. The current directory is the users home directory /home/myUser.

How does the POJO get the context's root directory or some similar path, so that it can read the properties file?

Thanks in advance

+1  A: 

The /WEB-INF/classes is just part of the classpath. You could obtain it as classpath resource by ClassLoader#getResourceAsStream(). In a webapplication, the best is to obtain the ClassLoader by Thread#getContextClassLoader() of the current Thread.

So, in a nut:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
properties.load(classLoader.getResourceAsStream("filename.properties"));
BalusC
A: 

and one more thing
if you can have a POJO that can read a properties file...
i guess something is wrong in the preliminary design..
the P in POJO stands for Plain...

p01ntbl4nk
On the other hand, it can also be the term "POJO" that is been abused. This happens much more often than you'd think. Regardless, this should have been posted as a comment on the question, not as an answer. If you have earned enough reputation by posting real answers, you'll be able to post comments on other's questions/answers.
BalusC