Tomcat + java.io.FileNotFoundException

A

apatruduque

Hello everybody,

I have a question about how to access a file from a .jsp. In summary:
fileA.jsp uses classA in /WEB-INF/classes to read /xml/fileB.xml (all
paths relative to tomcat) classA uses the File class to access
fileB.xml. To put you into context, I´m new to java but have some
experience configuring tomcat. Environment: Debian Linux with tomcat
4.0 and jre_1.4.2

The problem is that I'm constantly getting filenotfoundException.
After googling for a while I found out the following:

1- the File class uses paths relative to the working directory. I
temporarily solved it by using the absolute path /var/lib/.... But
this is not a solution since I have to deploy the application in
different OS´s (Linux, Windows and Mac) and I´m not changing the path
every time I deploy.

2- Reading the Tomcat FAQ I found out that to read a file I must use
the ServletContext.getResourceAsStream() method. However, classA is
not a Servlet...

3- I made classA derived from HttpServlet and I used the
ServletContext().getRealPath("/xml/fileB.xml") to obtain the path and
this path to open the file. I also tryed getResourceAsStream() with
success.

My question is: How can I access fileB.xml from classA without
deriving it from the servlet class? Am I making something wrong with
the design?

Thanks a lot in advance,

Alfonso
 
S

Sudsy

apatruduque wrote:
3- I made classA derived from HttpServlet and I used the
ServletContext().getRealPath("/xml/fileB.xml") to obtain the path and
this path to open the file. I also tryed getResourceAsStream() with
success.

My question is: How can I access fileB.xml from classA without
deriving it from the servlet class? Am I making something wrong with
the design?

You can have classA as a regular Object which takes a String argument
in the constructor which names the data file. You can then perform the
mapping (getRealPath) in the JSP and pass the result to the constructor.
 
A

Alex Kizub

Application Server executes servlet and JSP in their own context (both
different by the way).
And patrh /xml/abc/xyz is added to this context root.
It is better then read files not from file system but as resources like
this:
public String stringFromResource(String name) {

String result = "";
try {
URL url = getClass().getResource(name);
URLConnection conn = url.openConnection();
InputStream istream = conn.getInputStream();

BufferedReader in = new BufferedReader(new
InputStreamReader(istream));
String temp = "";
while ((temp = in.readLine()) != null)
result += temp + "\n";
in.close();
//System.out.println(result);
} catch (Exception e) {
System.out.println(e);
}
return result;
}

At least you know where is your resource root (exactly where is root for
class loading).

But if it's confusing for you I suggest to understand where are you in
file system and then put your files to this place.
Use regular java.io.File and run it INSIDE container! So you can
understand where you are and read files later:

java.io.File file=new java.io.File(".");
or
java.io.File fileRoot=new java.io.File("/");
and then read files in your location and try to understand where it is
eaxctly:

out.println(fileRoor.getAbsolutePath() +" "+fileRoot.getCanonicalPath());

or even this way:
String [] list=fileRoot.list();
if (list!=null) for (int i=0;i<list.length;i++) out println(list);

Alex Kizub.
 
A

apatruduque

Alex and Sudsy, thanks a lot for your suggestions. I'll try with resources,
since I have found a couple of places (one of them is the tomcat FAQ) in
which it is suggested to use resources to read files.

The problem is that the application was running smoothly using

File xyz= new File("[application-context-path]/fileB.xml");

However, after a controled shutdown and restart of the system (did not touch
any configuration file), I started getting the FileNotFound exceptions I
mentioned before, which is kind of "strange", and I cannot find an
explanation... may be the .class file being used was not corresponding to
the .java I had in the system.

The line before results in an error in catalina.out of the form:
java.io.FileNotFoundException: file "/[application-context-path]/fileB.xml"
not found

which means that the File constructor is trying to access the / (root)
directory of the filesystem. I put file fileB.xml
in /[application-context-path]/fileB.xml (file system path) and the
exception desapeared. This is kind of strange for me, since I thought (and
probably wrong) that tomcat somehow wraps everything inside the context
path...

For these reasons, I'm trying to figure out if:
1- I was not accessing the file correctly, which apparently is the case
2- Is there a way to tell tomcat to add the real path somehow to the File
constructor

Thanks again,

Alfonso
Application Server executes servlet and JSP in their own context (both
different by the way).
And patrh /xml/abc/xyz is added to this context root.
It is better then read files not from file system but as resources like
this:
public String stringFromResource(String name) {

String result = "";
try {
URL url = getClass().getResource(name);
URLConnection conn = url.openConnection();
InputStream istream = conn.getInputStream();

BufferedReader in = new BufferedReader(new
InputStreamReader(istream));
String temp = "";
while ((temp = in.readLine()) != null)
result += temp + "\n";
in.close();
//System.out.println(result);
} catch (Exception e) {
System.out.println(e);
}
return result;
}

At least you know where is your resource root (exactly where is root for
class loading).

But if it's confusing for you I suggest to understand where are you in
file system and then put your files to this place.
Use regular java.io.File and run it INSIDE container! So you can
understand where you are and read files later:

java.io.File file=new java.io.File(".");
or
java.io.File fileRoot=new java.io.File("/");
and then read files in your location and try to understand where it is
eaxctly:

out.println(fileRoor.getAbsolutePath() +" "+fileRoot.getCanonicalPath());

or even this way:
String [] list=fileRoot.list();
if (list!=null) for (int i=0;i<list.length;i++) out println(list);

Alex Kizub.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top