I have Spring application in which i am trying to read xml file from jar which is in lib folder of my build application war ie /WEB-INF/lib/abc.jar.
Right now i am using this code.
InputStream reader = SomeClassInabcJars.class
.getResourceAsStream("/META-INF/spring/beans.xml");
After this i am using DOM parser to get all bean tags from it.
If i deploy this application using command mvn tomcat:run, i am getting proper beans.xml. But if i deploy war of this application manually in tomcat then it seems there are some new bean tags in beans.xml and some bean tags are missing from it.
If i extract this war and open that abc.jar's beans.xml, it seems correct. I think this beans.xml changes at runtime.
I found a solution to read any file from jar which is in lib folder of a build application war file and it worked for me.
InputStream reader = null;
for (URL url : ((URLClassLoader) (Thread.currentThread()
.getContextClassLoader())).getURLs()) {
if (url.getPath().contains("notification-engine")) {
String inputFile = "jar:file:" + url.getPath()
+ "!/META-INF/spring/beans.xml";
URL inputURL = null;
try {
inputURL = new URL(inputFile);
JarURLConnection conn = (JarURLConnection) inputURL
.openConnection();
reader = conn.getInputStream();
} catch (MalformedURLException e1) {
System.err.println("Malformed input URL: " + inputURL);
} catch (IOException e1) {
System.err.println("IO error open connection");
}
break;
}
}
It read /META-INF/spring/beans.xml file from path(its a jar path) contain notification-engine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.