• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

getRealPath returns null in Linux(Fedora)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
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).
 
B Bhutta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic