I have two jar files (Lets say jar1 and jar2). There is one xml file inside a jar2. I want to read the xml file. i used
public void readXmlFile(){
InputStream resourceAsStream = MainFile.class.getResourceAsStream("/test.xml");
}
But now i am calling this function frm a class in jar1 using
File file = new File(jar2);
URL url = file.toURL();
URL[] urls = new URL[] { url };
ClassLoader cl = new URLClassLoader(urls);
Class<?> compositeClass = cl.loadClass(XmlFileReader);
Method declaredMethod = compositeClass.getDeclaredMethod("readXmlFile");
Object newInstance = compositeClass.newInstance();
declaredMethod.invoke(newInstance);
Now I am getting FileNotFoundException as the xml file is being searched in jar1 and not in jar2, I do not know why is this happening. Can anyone help me?
Th Only solution i found is to set a System property using System.setProperty(key, value) in my jar1 as the path of the folder containing my both the jars. Then i read the system property back in my jar2 and then modified my readXml method like this