R – How to one add an XML file in classpath at runtime in java

classpathrelative-pathruntimeurl

I am not sure if a URLClassLoader can be used. Also, only the relative path of the XML is known.

Thank You.

Best Solution

If you just mean read in an XML file that is already on the classpath, so that you can parse it using whatever library you prefer, the most compact way is using a ClassLoader:

InputStream is = ClassLoader.getResourceAsStream();
// use the input stream however you want
is.close()

See http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html