I’ve just been getting started setting up a web service with jax-rs running on Tomcat. Is there a way to bundle a properties file with my java project (in eclipse) so that I can read properties out of it at runtime? Also if it’s possible, where would be the best location to put it (so that it couldn’t be seen via a url), WebContent, WEB-INF, etc?
Thanks.
Several options:
Option 1: You can put it under your classpath (in Eclipse put it under and source folder), so you can access it via the Classloader:
MyClass.class.getResourceAsStream("myproperties.properites")Pay attention that MyClass must also be in the same source folder (actually it’s a bit more complex, it must be in the same classloader hierarchy, but any class from the same folder will do the job)
Option 2: Put it in WEB-INF folder. It’s a preferred option, since you don’t need to deal with the classpath. You’ll need a ServletContext to access it:
javax.servlet.ServletContext.getResourceAsStream("WEB-INF/myproperties.properites")In jax-rs you can obtain the ServletContext using the @Context annotation in any registered resource or provider.