Hi there!
I am not sure to be right here at this forum, but:
Yes, this is a reasonable place to ask your question.
I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!
I have defined the Tomcat4 for MyEclipse with different HOME and BASE,
if I set a fix path (i.e. "c:/file.ext") then everythin works fine, but
without the path, the FileInputStream everytime gives a exception:
java.io.FileNotFoundException: file.ext
I have placed the file.ext in many directories, but it never has been
found!
Where to hell does Tomcat search for this file???
Please could someone help me? Do I have to place the file in
TOMCAT_HOME, TOMCAT_BASE, somwhere under one of this, or what?
I haven't written a servlet in a couple of years so my memory is a bit hazy
for where Tomcat4 looked for files. However, I've looked at my first
servlet, which does some file I/O, and my old Tomcat4 setup, and I _think_
your servlet will look for the file in the webapps folder. Within the
webapps folder, you will have a variety of other folders, one of which
should have the same name as your servlet; I think the file you want to be
read should be in that folder.
For example, if your servlet is called Foo, go to your tomcat directory; in
my case, this is d:\jakarta-tomcat-4.1.29. Then enter the webapps directory
within Tomcat. Then enter the Foo directory within the webapps directory. I
_think_ that Tomcat will find your file if you put it there and then simply
ask for it by its simple name, e.g. file.ext.
However, if that does not work, add this code to your servlet (temporarily)
to find out where the servlet wants the file to be:
String realPath =
this.getServletConfig().getServletContext().getRealPath("/");
log ("realPath=" + realPath);
Execute the servlet in Tomcat, or at least enough of it that those two lines
execute. Then, find your log file and see what it has told you about the
value of 'realPath'. I honestly don't remember where Tomcat4 writes the log
any more: I think it is probably catalina.out but it may have been
localhost_log.2005-12-30.txt or one of the other files in the logs directory
of Tomcat. In any case, the value of 'realPath', wherever it is written,
_should_ be the directory where it was looking for the file. Just place the
file in that directory and this time your servlet should find the file.
(Naturally, you can remove the two lines I suggested after executing the
servlet the first time.)
From then on, all references to external resources like files should be made
relative to the value given to you by 'realPath'. Therefore, if realPath is
d:\jakarta-tomcat-4.1.29\webapps\Foo and you want to put the file in
d:\jakarta-tomcat-4.1.29\webapps\Foo\Resources\Files\file.ext, I think you
should be referring to the file as "Resources/files/file.ext"; you should
_not_ refer to it by the absolute path.
For more information about this subject, consult a good servlet programming
book. When I started writing servlets in 2002, several people recommended
Jason Hunter's Java Servlet Programming, Second Edition. I'm not sure if
anything better has come along since so you might want to ask on this
newsgroup to see.
Rhino