• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Igor Gumski
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Venkatraman Kandaswamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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.
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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.
 
Ray Johnson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Tanmoy Palodhi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic