Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a webapp. I want to load a properties file, but I dont want to hardcode the full path of the properties file. I want to use relative paths under the webapp root. If I load a resource on the classpath such as under /WEB-INF/classes/app.properties, that works fine. But what if the properties file is under a different directory like /WEB-INF/configfiles/ ?

I also don't want to specify /WEB-INF/configfiles/ as part of the classpath when starting up the webapp. Is there a way to do what I want? Thanks.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try having your config files outside WEB-INF. If you have your files in your-webapp/config folder - you can access the property file in your code as config/properties-file.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Venkatraman Kandaswamy:
You can try having your config files outside WEB-INF. If you have your files in your-webapp/config folder - you can access the property file in your code as config/properties-file.


when the configuration file is stored under webapp, then it will be accessed easily by anyone who specifies the url to that file, Which is a security leak, if the configuration file has data base information or some other sensitive data.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure of the benefits of having property file outside the classes folder, though it is secure & straightforward. However I am thinking of other ways that you can try (though I myself feel weird of my suggestions :roll: Again it is classpath - inside a jar file in /web-inf/lib. Otherwise keep it in web-inf/config folder and publish it through a dedicated & secured servlet other than your controller. If you are using framework like struts you can do it easily through an action path whose url pattern is protected from public. (have to try out and see)
If you could share the reason for keeping the property file outside classes folder, it might be useful for others.

[ February 15, 2008: Message edited by: Ray Johnson ]

[ February 15, 2008: Message edited by: Ray Johnson ]
[ February 15, 2008: Message edited by: Ray Johnson ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use context.getResource or context.getResourceAsStream, both of which take a context relative path for an argument.
 
Igor Gumski
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is we have properties files and xml config files (lots actually) so we dont just want to dump them under the /WEB-INF/classes folder. So I tried putting them outside but then cant find them anymore in classpath. Dont want to use a startup parameter to add to the classpath either. Main reason to put it outside the /classes folder or /lib is for clarity of organization.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also looking for some kind of solution to this problem. I want to keep my .properties files outside the web-app. I have created a new env variable and set the classpath in the server's Startup.sh batch file.
I called the .properties file by using the below code:

InputStream inputStream = getClass().getResourceAsStream("abc.properties");
logger.info("InputStream is: " + inputStream);

The value printed is 'null'.

Can anyone help?

Thanks.
Tanmoy
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is it supposed to know where the file is? YOu need to provide the path to the file. Perhaps as a context parameter?

Classpath and environment variables will be of no help in your situation.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic