• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting Base directory

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble getting my tomcat base directory set. I'm thinking it's a path issue but not sure.
I can't get my servlet to read a text file in my tomcat directory. I've moved it every where I can think of to get it to read but it's not finding it.

So I did a test and put a line of code in my servlet;

File dir = new File ("exam" + File.separator + "files");
dir.mkdir ();

It puts the new dir in Window/System32.
How do I get it to put it in my tomcat dir?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should never make any assumptions about what the working folder for a web app is. And you cannot set it.

So when dealing with files from a web app you need to specify an absolute file path.

You can obtain the absolute folder paths for resources within a web app using the ServletContext.getRealPath() method, assuming that your app isn't runnig out of an unexpanded war file.
 
Jim Bauer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what I'm asking is this:
I can make my servlet work when I specify file name "C:\\My Document\\file.txt"

But when I move it up to my host provider in my webapps folder, how do I specify where to find the file, since it won't be on my machine anymore? Does this need to be done in my web.xml, and if so how?
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic