In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it?
A:
That depends on where exactly the XML file is. Is it in the sources folder (in the "default package" or the "root") or in the same folder as the class?
In for former case, you must use "/file.xml
" (note the leading slash) to find the file and it doesn't matter which class you use to try to locate it.
If the XML file is next to some class, SomeClass.class.getResourceAsStream()
with just the filename is the way to go.
Aaron Digulla
2009-04-27 12:26:29
+1
A:
ClassLoader.getResourceAsStream("/path/to/your/xml")
and make sure that your compile script is copying the xml file to where in your CLASSPATH.
Clint
2009-04-27 13:43:56