#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0

    Question File I/O from servlet


    My servlet calls another java class that reads configuration information from an external xml file and returns it to the servlet. This other class resides in a subdirectory of my web application. I do not want to include this other xml file's data in the web.xml,
    AND I DON'T WANT TO HARD-CODE THE PATH to the xml file, but unless I do, my app can't find it.

    My class contains the line
    protected static final String configXMLFileName = "myConfig.xml";
    ...
    later code opens the file and parses it.

    If I put the complete path in this variable, it works. Where can I drop this file into a 'default' directory where the app will be able to read it, without any path info in the filename? I've tried web-inf, the subdirectory where my class lives, 'C:\Program Files\Apache Group\Tomcat 4.1\bin', all to no avail. No matter what, I get:

    java.io.FileNotFoundException: myConfig.xml (The system cannot find the file specified)

    Thanks in advance for your help.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    If the class you are using resides in a folder located in the WEB-INF/bin folder, then you can put the xml file in the root of the bin folder and access it using the "/myConfig.xml" path. If you want, you can put the xml file in the folder the actual calss resides in and then you can use the file name without any path information.

    For example, if the class is in a package called com.mystuff.myclass, then you could put the xml file in WEB-INF/bin/com/mystuff and access it simply by using the myConfig.xml file name in the FileInputStream or whatever.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0
    Thanks for your help.
    All of my servlet application classes reside under WEB-INF/classes. I have tried putting the xml file in the same folder as the class, and refering to it in code as "/myConfig.xml" and "myConfig.xml" (with and without the slash), all to no avail.

    The only thing I've found to work is to drop the file into the launch directory of Tomcat, "C:\Documents and Settings\Administrator\Start Menu\Programs\Apache Tomcat 4.1". But that makes the file global to the webserver, not what I want. I want the file to be global only to a servlet context / web application.

    What have I missed? Is there some special significance to WEB-INF/bin? I haven't heard of that before. Where can I learn more?
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Sorry, I meant "classes", not "bin". I was having a mental lapse.

    I don't know why you can't find the file if you have put it in the same folder as the class file and referred to it by name. This works for me in my application, but I am not using Tomcat. I have Websphere 5.0.
    Last edited by Nemi; March 19th, 2003 at 11:09 AM.
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I take that back. I use getClass().getResourceAsStream("/filename.xml"), and not just a FileInputStream. This is likely your problem. Using the classes Class.getResourceAsStream looks for the file relative to the location of the class file you are using. Otherwise you would have to supply a system path.
    Last edited by Nemi; March 19th, 2003 at 11:25 AM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    6
    Rep Power
    0
    Whew! I got it! It works, without the leading slash, using the following line:
    InputStream fis = new myConfig().getClass().getResourceAsStream ( "xmlFileName" );

    The file can now reside in the same directory with the class that reads it.

    Thanks for going the extra mile for me.
  12. #7
  13. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    No problem.

    You can save the creation of that object by using (I am assuming your class name is myConfig)
    Code:
    myConfig.class.getResourceAsStream ( "xmlFileName" );

IMN logo majestic logo threadwatch logo seochat tools logo