Re: Tomcat + java.io.FileNotFoundException
From: apatruduque (apatruduque_at_yahoo.es)
Date: 09/14/04
- Next message: Paul Lutus: "Re: How to display multiple images"
- Previous message: John Broadley: "How to display multiple images"
- In reply to: Alex Kizub: "Re: Tomcat + java.io.FileNotFoundException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Sep 2004 09:07:33 +0200
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[i]);
>
> Alex Kizub.
- Next message: Paul Lutus: "Re: How to display multiple images"
- Previous message: John Broadley: "How to display multiple images"
- In reply to: Alex Kizub: "Re: Tomcat + java.io.FileNotFoundException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|