ashlrem ashlrem - 11 months ago 105
Java Question

Reading inside .war file from url using java

I am trying to read the MANIFEST.MF file from inside the file.war.. It is inside the folder META-INF.
I have this code, but I am getting null pointer exception. Advise please? Thank you!

String warurl = "http://.../file.war";

URL url = new URL(warurl);
InputStream inputStream = url.openStream().getClass().getResourceAsStream("META-INF/MANIFEST.MF");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

}catch(Exception e){
System.out.println("Error reading manifest file.");

Answer Source

You're trying to retrieve a resource from the classloader of the InputStream of the URL: openStream().getClass() - that's not the same as getting it from the war file that the URL points to.

You should use a JarURLConnection. An example from the Javadoc of this standard API class:

URL url = new URL("jar:file:/home/duke/duke.jar!/");
JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
Manifest manifest = jarConnection.getManifest();

This also gives you a handy Manifest object to retrieve data from the manifest.