12

So I'm trying to add some ability to my project to allow user-defined properties in my deployment artifact - a simple key:value .properties file. I place the service.properties file in

war/WEB-INF/my-service.properties 

And in my ServiceImpl.java constructor I have the following:

String propertiesFileName = "my-service.properties"; 

URL propertyURL = ClassLoader.getSystemResource(propertiesFileName);
URL propertyURL2 = this.getClass().getClassLoader().getResource(propertiesFileName);
URL propertyURL3 = this.getClass().getClassLoader().getResource( "WEB-INF/" + propertiesFileName);
URL propertyURL6 = this.getClass().getClassLoader().getResource(
       "E:/Projects/eclipse-workspace/projectName/war/WEB-INF/" + propertiesFileName);

All instances of Property URL are null. I know I'm missing something absolutely obvious, but I need a second pair of eyes. Regards.

EDIT:

Ah, it seems I was confused as the default GAE project creates a logging.properties file in /war. From the Google App Engine documentation:

The App Engine Java SDK includes a template logging.properties file, in the appengine-java-sdk/config/user/ directory. To use it, copy the file to your WEB-INF/classes directory (or elsewhere in the WAR), then the system property java.util.logging.config.file to "WEB-INF/classes/logging.properties" (or whichever path you choose, relative to the application root). You can set system properties in the appengine-web.xml file, as follows:

Jonik
  • 80,077
  • 70
  • 264
  • 372
Chris K
  • 11,996
  • 7
  • 37
  • 65
  • I assume it is a typo in your question rather than your app, but you state that your file is called "service.properties" yet propertiesFileName is set to "my-service.properties"! – Todd Owen Sep 01 '09 at 14:22
  • @Todd: Yes, it's a typo, I'll correct it. – Chris K Sep 01 '09 at 14:41

3 Answers3

10

Try putting the service.properties in WEB-INF/classes. Then it should be accessible just with :

this.getClass().getClassLoader().getResourceAsStream("/filename.properties");
jsight
  • 27,819
  • 25
  • 107
  • 140
  • 1
    Do you know why for me I needed to remove the "/" at the start of the path for it to work? – Mike Jul 29 '15 at 22:01
  • I'm receiving an NPE. I'm attempting to access the file (*kotlin/ webapp/WEB-INF/fileName.json*) from my class (*kotlin/ClassName*) `val credentials = GoogleCredentials.fromStream(this.javaClass.getClassLoader().getResourceAsStream("/coinverse-media-staging-firebase-config.json"))`. – AdamHurwitz Oct 21 '18 at 22:26
1

As Mike mentioned in his comment to jsights answer, it worked for me if I used

this.getClass().getClassLoader().getResourceAsStream("filename.properties");

(removed the first slash) after placing the file in WEB-INF/classes.

Community
  • 1
  • 1
Erik Živković
  • 4,867
  • 2
  • 35
  • 53
0

I think what you will need is something like this:

String filePath = servletContext.getRealPath("/WEB-INF/views/") + "/" + mav.getViewName() + ".vm"; FileInputStream in = new FileInputStream(filePath);

I get the servletContext from spring: @Autowire ServletContext.

Rafael Sanches
  • 1,823
  • 21
  • 28