• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets and relative file path io

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a problem opening a file using the BufferedReader and I'd post the code except for the fact that it's 14 miles long. Basically, I am using a Servlet and JSP through Tomcat 5.0, and I am trying to get my Servlet to open a file on the server side and parse it. Here is the line I'm using:

try
{
BufferedReader inFile = new BufferedReader(new FileReader("Courses.txt"));
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found exception:\n" + fnfe.getMessage());
}

That sniplet of code, as well as Courses.txt, is located in a server-side Servlet in the directory of:

"C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp\WEB-INF\classes\CoursePackage"

The JAVA file is indeed part of a package (CouresPackage) and is being used by Tomcat. For some reason, everytime that code executes, the FileNotFoundException is thrown and caught, and the following message is given:

Courses.txt (The system cannot find the file specified)

The weird part is, if I change the parameter from "Courses.txt" to "C:\\Courses.txt" (and place Courses.txt directly on the C drive), the FileNotFoundException is never thrown and all works fine. However, I want to use a relative file path. Consequently, I have a copy of Courses.txt in nearly every directly under Tomcat, but still the file is never found when the program executes.

Where is JAVA/Tomcat looking to find this file??
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The relative path of a Servlet is pointed to the root directory of your Web Application.

Nick
 
Charlie Tronolone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, in my example, if I were to put Courses.txt in the directory of:

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp

then it should work, right? It hasn't worked. I have Courses.txt copied to:

C:\Program Files\Apache Software Foundation\Tomcat 5.0
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp\WEB-INF
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp\WEB-INF\classes
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myTestApp\WEB-INF\classes\CoursePackage

and STILL it can't find the file. It's looking elsewhere and I don't know where... what is up with this?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charlie,

The servlet will try to pick up file from the "current execution directory" which is usually the directory where the webserver starts from. In Tomcat it would be Tomcat/bin folder as the start server script is located there.

It works when you type c:\\filename because its absolute path.

If you want relative path and you have the file in the web-inf/classes/yourpackage you can use getResourceAsStream() instead which looksup for the file in the CLASSPATH and you would have the WEB-INF/classes in the classpath by default. Now, as your servlet is in "yourpackage" and it should fine the file located in WEB-INF/classes/yourpackage/.

Try the following and see if it works for you,
BufferedReader br = new BufferedReader(new InputStreamReader(YouServletClassName.class.getResourceAsStream("yourfile.txt")));

Regards
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicholas,

I think your assumption is not correct. Relative path of the file would be the webserver process's start directory as I mentioned in my previous post.

On Tomcat its Tomcat/bin
On iPlanet webserver its config folder...

I am positive about this. (If you read my FAQ linked below you would find this question ) )

Regards
Maulin
 
Charlie Tronolone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maulin, thank you. That had been driving me crazy. I'll have to remember that trick in my future endeavors. If I could kiss you, I might. ehh... probably not - but the thought may cross my mind. You probably don't care.

... and there was much rejoice.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charlie,

No problem, keep that workings of webserver in mind and make sure you can help anybody else on this in future. Thats all reward for me and Javaranch.

Btw, I found this one by printing property- user.dir via System.out.println(System.getProperty("user.dir")); When first time I hit this issue...

These are little things, but give much pain when first time hit.

Regards
Maulin

[ April 04, 2005: Message edited by: Maulin Vasavada ]

[ April 04, 2005: Message edited by: Maulin Vasavada ]
[ April 04, 2005: Message edited by: Maulin Vasavada ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another trick would have been to do

System.out.println(new File("Courses.txt").getAbsolutPath());
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic