Search code examples
javaxmlclassloaderstatic-block

Loading xml file from another project


I have written a java class with static block to read a xml in project A. Also i have added a dependency of project-A into B which needs data from xml. But when i try to read the xml using the readerclass in A, am getting an error saying the file not found exception as "...../B/web-inf/lib/A.jar!/data.xml". As the xml file is inside the reader jar, I am unable to proceed. The static block inside project A is getting executed only during first request from project-B,

Project A code:

static {
...
//to read xml data
ClassLoader classLoader = ErrorInfoReader.class.getClassLoader();
File xmlFile = new File(classLoader.getResource("data.xml")
                .getFile());
Document document = (Document) builder.build(xmlFile);

...
}

Please help me to resolve this....


Solution

  • Used InputStream to read Xml and it worked.

    InputStream stream = classLoader.getResourceAsStream("data.xml"); Document document = (Document) builder.build(stream);