GeeCON Prague 2014*
The moose likes Java in General and the fly likes Cant read XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Cant read XML file" Watch "Cant read XML file" New topic
Author

Cant read XML file

Pappu Kumar
Ranch Hand

Joined: Oct 01, 2002
Posts: 69
I have a XML file which i put in the WEB-INF/CLASSES folder , but when i run my code that tries to read this xml file , Here is the snippet of the code.
************************************************
Document doc = XMLUtil.getDocument(new InputSource((new Object()).getClass().getResourceAsStream("/" + "Trial.xml")));
*************************************************
, it always looks for the file in my c:\program files\apache group\Tomcat 4.1 directory.....ultimately i will be running all code on UNIX and i wont have access to directories outside my web-app , so how do i solve this issue ?? Please help
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Class.getResource() searches for resources in the classpath. It uses the the classloader using which ur class was loaded to do this. So if ur resource is reachable by the classloader meaning if it is in the classpath, then there should be no problem. How do you know that it is searching for the resource in the tomcat folder??
Regards
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
and more way to read resources would be to use ServletContext.getResourceAsStream(path). This will search in your web context directory.
regards.
Pappu Kumar
Ranch Hand

Joined: Oct 01, 2002
Posts: 69
How do i know its searching there ??
because it says...cant find
c:\Program files\Apache Group\Tomcat 4.1\Delegator.xml
and i am not reading through a servlet , a Utility class is reading it
Pappu Kumar
Ranch Hand

Joined: Oct 01, 2002
Posts: 69
i meant
c:\Program files\Apache Group\Tomcat 4.1\Trial.xml
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Originally posted by Pappu Kumar:
i meant
c:\Program files\Apache Group\Tomcat 4.1\Trial.xml

from the javadocs for Class.getResourceAsStream()
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
So this method would return null, if the resource is not found. Thats the resaon I asked how du know that the resource is being searched in your tomcat folder. I think your utility class is misleading you with the message you are getting. Try to run the program without the utility class.
Regards.
 
GeeCON Prague 2014
 
subject: Cant read XML file