Re: Tomcat + java.io.FileNotFoundException
From: Alex Kizub (akizub_at_yahoo.com)
Date: 09/14/04
- Next message: WJ: "Re: Creating Logger in base class"
- Previous message: Alex Kizub: "Re: Successor to Java?"
- In reply to: apatruduque: "Tomcat + java.io.FileNotFoundException"
- Next in thread: apatruduque: "Re: Tomcat + java.io.FileNotFoundException"
- Reply: apatruduque: "Re: Tomcat + java.io.FileNotFoundException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Sep 2004 01:30:11 GMT
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[i]);
Alex Kizub.
apatruduque wrote:
> 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
apatruduque wrote:
> 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
- Next message: WJ: "Re: Creating Logger in base class"
- Previous message: Alex Kizub: "Re: Successor to Java?"
- In reply to: apatruduque: "Tomcat + java.io.FileNotFoundException"
- Next in thread: apatruduque: "Re: Tomcat + java.io.FileNotFoundException"
- Reply: apatruduque: "Re: Tomcat + java.io.FileNotFoundException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|