File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Setting Base directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Setting Base directory" Watch "Setting Base directory" New topic
Author

Setting Base directory

Jim Bauer
Greenhorn

Joined: Nov 07, 2005
Posts: 9
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jim Bauer
Greenhorn

Joined: Nov 07, 2005
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting Base directory
 
Similar Threads
Servlet compilation/loading
UnpackWAR
Asking you the two problems:
where to put my "mysql-5.0.3-bin.jar" file
Deploying a webapp in Tomcat5.5 manager