aspose file tools*
The moose likes Servlets and the fly likes How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?" Watch "How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?" New topic
Author

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

Igor Gumski
Greenhorn

Joined: Apr 06, 2006
Posts: 3
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

Joined: Jul 07, 2004
Posts: 120
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.


--Venkatraman<br />SCJP 1.4<br /><a href="http://kvrlogs.blogspot.com" target="_blank" rel="nofollow">blog</a>
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
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.


Do you know why this cup is useful? Because it is empty.
Ray Johnson
Greenhorn

Joined: Dec 14, 2007
Posts: 13
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 ]

"It's been so long since I've done a good thing--the right thing" : Tears Of Sun
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can use context.getResource or context.getResourceAsStream, both of which take a context relative path for an argument.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Igor Gumski
Greenhorn

Joined: Apr 06, 2006
Posts: 3
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

Joined: May 21, 2007
Posts: 13
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

Joined: Jan 10, 2002
Posts: 61202
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?