ashlrem
ashlrem

Reputation: 127

Reading manifest.mf 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";

try{
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.");
}

Upvotes: 0

Views: 1180

Answers (1)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

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.

Upvotes: 1

Related Questions