2

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.

Blaskovicz
  • 6,122
  • 7
  • 41
  • 50
  • 1
    Just put it in the classpath (WEB-INF/classes; in a maven project, src/resources is the appropriate location) and load it with the classloader. – Artefacto Oct 26 '11 at 14:00
  • I don't have a maven project, would WEB-INF/lib suffice? What would the path look like at runtime for the file Properties("WEB-INF/lib/jaxrs.properties") – Blaskovicz Oct 26 '11 at 14:03

2 Answers2

7

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.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • If my class isn't a Resource class that's being returned, rather it is a POJO that's being created (extends Authenticator), would I need to physically pass the servlet context from the resource into it's constructor or via a setter or is there a way to inject it there as well? – Blaskovicz Oct 26 '11 at 14:54
  • Well, if a class is not created via JAX-RS, then JAX_RS won't help you here. You'll need to do it manually. – Tarlog Oct 26 '11 at 15:00
  • I had to do something like Someclass.class.getResourceAsStream("../../file.properties") but this will need to change if a class is in another package. Is there ta way I can specify an absolute path to the file, ie /src/file.properties or something? – Blaskovicz Oct 26 '11 at 15:12
  • Just put the properties file under the root package, so you can always write: Someclass.class.getResourceAsStream("file.properties") – Tarlog Oct 26 '11 at 15:55
  • What about the case where I need to write the file? – Blaskovicz Oct 26 '11 at 17:00
  • What about this case? What exactly do you want to know? If you need the file location, you can use getResource instead of getResourceAsStream, so you get the file's URL. The you can treat it just as a regular file. – Tarlog Oct 27 '11 at 06:59
1

For GlassFish 3.1 users, I was able to get both of Tarlog's options working, but I had to use the file's absolute path. For Option 1 I put the file in the Source Packages folder in NetBeans, which I could then access via:

InputStream is = TestResource.class.getResourceAsStream("/test_model.js");

For Option 2 I put the file under WEB-INF and used:

@Context ServletContext servletContext;
InputStream is = servletContext.getResourceAsStream("/WEB-INF/test_model.js");

Without the leading slash the result was null. HTH

Matthew Cornell
  • 4,114
  • 3
  • 27
  • 40
  • Is there a way to use a property file or some custom setting file to my war that will be loaded on start but is not inside the project? like for example /etc/myapp/file.conf – Asaf Magen May 05 '20 at 17:17