GeeCON Prague 2014*
The moose likes Servlets and the fly likes FileReader - file not found. What am I missing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "FileReader - file not found. What am I missing?" Watch "FileReader - file not found. What am I missing?" New topic
Author

FileReader - file not found. What am I missing?

Stu Quinn
Ranch Hand

Joined: Feb 27, 2009
Posts: 35
Hello,

I want to pull in an xml file, however I seem to be having difficulty with the relative path. The file is on the root of src and is therefore replicated into the root of classes, however this fails indicating the file reference is invalid.

I am within a war (within an ear) however I am not in a servlet so I cannot take advantage of servletContext.

The following code fails indicating file not found. Any suggestions?


String path = "/WEB-INF/classes/";

String fileName = "SearchCriteria.xml";

try {
Reader reader = new FileReader(path + fileName);
.
.
.

This is on WebSphere 6.0, jdk 1.4 (yes this is old, but I am promised a shiny new WAS 7.0 soon).


Thanks,
Stu
Deepak Pant
Ranch Hand

Joined: Feb 13, 2004
Posts: 443
You can try following code to read the data file (XML, txt, properties etc) present in WAR or JAR file.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Deepak's approach of using getResourceAsStream() is preferred over "normal" file I/O for two reasons:

  • If the web app is deployed as a war file, there is no file system in the first place.
  • Web app's have no concept of a "current directory", so relative addressing of file paths is not possible.

  • If you must use file I/O for some reason (and aren't deployed as a war file), you need to find the absolute file path with ServletContent.getRealPath().


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Rene Larsen
    Ranch Hand

    Joined: Oct 12, 2001
    Posts: 1179

    Deepak Pant wrote:You can try following code to read the data file (XML, txt, properties etc) present in WAR or JAR file.



    Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??


    Regards, Rene Larsen
    Dropbox Invite
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61315
        
      66

    Rene Larsen wrote:Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??

    That would be an odd place to put an XML file. Config files would customarily be placed in WEB-INF, or a folder under it (other than classes or lib).
    Rene Larsen
    Ranch Hand

    Joined: Oct 12, 2001
    Posts: 1179

    Bear Bibeault wrote:
    Rene Larsen wrote:Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??

    That would be an odd place to put an XML file. Config files would customarily be placed in WEB-INF, or a folder under it (other than classes or lib).

    The /WEB-INF/classes folder can be used - but the path used for getting the xml file, was what I meant

    Instead of:
    I would use:
    Deepak Pant
    Ranch Hand

    Joined: Feb 13, 2004
    Posts: 443
    I think the exact location of the xml file was not the problem. I wanted to show sample code on how to access files bundled in WAR or JAR so I kept the same path as what Stu had in the first post.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61315
        
      66

    Ah yes, the difference between Class.getResourceAsStream() and ServletContext.getResourceAsStream().

    Using the former requires the file be on the classpath; the latter as relative form the context root.
    Deepak Pant
    Ranch Hand

    Joined: Feb 13, 2004
    Posts: 443
    Yes thats true as well. Also "WEB-INF/classes/" is in the classpath so keeping "SearchCriteria.xml" file in there will make it easier.
    Stu Quinn
    Ranch Hand

    Joined: Feb 27, 2009
    Posts: 35
    Thank you for your recommendations. This works great. I had originally placed the file in \classes because there are some other related files at the root of \src and hence are copied to \classes. Your comments are right on and I will move my file to \WEB-INF.

    I really appreciate the help.

    Back in the saddle,
    Stu
     
    GeeCON Prague 2014
     
    subject: FileReader - file not found. What am I missing?