This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRealPath returns null in Linux(Fedora)

 
B Bhutta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
Srivatsa Katta
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic