aspose file tools*
The moose likes Servlets and the fly likes getRealPath returns null in Linux(Fedora) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getRealPath returns null in Linux(Fedora)" Watch "getRealPath returns null in Linux(Fedora)" New topic
Author

getRealPath returns null in Linux(Fedora)

B Bhutta
Greenhorn

Joined: Jun 28, 2002
Posts: 14
Hi,
I need to access my drictory(s) on server in my tomcat application from servlets to list the files available in the drictory. Also, I need to upload files, for which I need to get the path too, but the method getServletContext().getRealPath("SomeDirectory") returns null. My application works perfectly fine under winodws, however, under fedora it returns null and it is installed as war file using tomcat. I can access other files in the same direcoty of war file. Please help me out. Here are some codes which I tested and the string path is always coming as null, althought getServletConfig() or getServletContext() are not null.

String path = getServletConfig().getServletContext().getRealPath("UploadedFiles/forms");

OR

String path = getServletContext().getRealPath("UploadedFiles/forms");

OR

String path = getServletConfig().getServletContext().getRealPath("UploadedFiles/forms");

OR

String path = getServletConfig().getServletContext().getRealPath(java.io.File.separator+"UploadedFiles"+java.io.File.separator+"forms"+java.io.File.separator);

After getting path value I would like to do:
File file = new File(path);
String[] myFiles = file.list();
...

Any help will be appreciated.
Thanks.
Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
Hi,

While giving the parameter for getRealPath().. try giving the parameter including ur context path.

For eg: if ur context path is /TestContext

try using

String path = getServletConfig().getServletContext().getRealPath("/TestContext/UploadedFiles/forms");


-Tripter
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

under fedora it returns null and it is installed as war file using tomcat.

You can't get a 'File' reference to something inside a WAR, nor can you upload 'into a WAR'.

Dave
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you deploy by dropping the war file into TOMCAT/webapps, Tomcat will unpack the war file and run the app from an exploded file system (getRealPath will work). If you deploy a war file with the manager from a different location it will not.

I've often posted agains relying on getRealPath on this and the JSP forum.
Doing so creates non-portable webapps. It's better to make the location of upload dirs, etc configurable with servlet or context init params (and keep the location outside of the webapp).


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

Joined: Jun 28, 2002
Posts: 14
Hi,
Thanks for the reply from everybody. I have tried even adding my application context name, that doesn't work too.

If we cannot access directories in war file using getRealPath.., do you know what changes to make in tomcat in fedora (linux) to make tomcat to explode the war file everytime new war file is uploaded, although my jsp pages are getting images and links for files under same directories in war file are working.

However, thanks for your help. I really appreciate.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getRealPath returns null in Linux(Fedora)
 
Similar Threads
Refactoring -- extracting hard-coded paths into a properties file for servlets
Reading File
getRealPath() returning null
how to get strut applications root directory
Hyper link Doubt