views:

2724

answers:

5

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?

+9  A: 

ClassLoader.getResourceAsStream().

cletus
+1  A: 

someClassWithinYourSourceDir.getClass().getResourceAsStream();

mP
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
+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