wood burning stoves 2.0*
The moose likes Servlets and the fly likes Why can't servlets access a file with a relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why can Watch "Why can New topic

Why can't servlets access a file with a relative path


Joined: Nov 29, 2000
Posts: 5
I have servlets that use properties files in the init method.
I want access the files with a pth relative to the class file but I can't do that. I can only hard code the fullpath name to the file. Any Ideas why that is? Or any ideas about how to circumvent that?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12760
Thats just the way it work, eh.
Use the web.xml file for your application to set a parameter that is read in the servlet init method to avoid having to hard code a path. (Thats assuming you are using an engine that is API 2.2 or later). Here is an example:

Joined: Nov 29, 2000
Posts: 5
That's a good idea. I've been searching for any solution to this one . Thanks!!!
kamal pasha shaikh

Joined: Aug 21, 2001
Posts: 1
hi bill,
please tell me how will the servlet identify where the xml file is kept if u r not specifying the absolute path.just as we had to specify the full hard coded path for the properties file so that servlet can access it and then read the key-value pairs from it,here we have a xml file instead of a properties file and this xml file will contain the path of the properties file as i understand.but again the problem is how will the servlet read the xml file using the relative path because that was the actual problem and if the servlet is able to read the xml file without giving the absolute path,then it should also be able to read the properties file without giving the absolute path.
well,please correct me if im wrong as i have stuck up with the same problem.
thanks in advance.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
If the property file exists under your webapp, you can use ServletContext to locate and load the file.
Example: web.properties is under /WEB-INF/properties for your webapp

Hope This Helps
Carl Trusiak, SCJP2

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Consider Paul's rocket mass heater.
subject: Why can't servlets access a file with a relative path
Similar Threads
2 jnlp files using the same extensions
Loading custom application config files
Accessing Files from a Web App
JASPER Compile Exception during JSP precompilation
Trusted and Signed Applet still gets java.io.filepermission read error