5

I am trying to read an own MANIFEST.MF resource in a Java servlet. My situation: I have a WAR (with the manifest I want to read) inside an EAR. There are several other WARs and JARs in the EAR. A class path is really long.

I tried several ways found in the Web, including StackOverflow.

I can read all MANIFEST.MF files using

this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");

and iterate through them. However, I do not know which one is mine - I do not know even Implementation-Title since this is generated by a build pipe. (I can guess with knowledge of the build pipe, therefore I know the correct manifest is there. However, I cannot guess in a production code.)

Of course,

this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");

returns a completely wrong manifest from some other jar on a class path.

I also tried

this.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF");

but it returns a null.

How to access a MANIFEST.MF file belonging to the WAR containing a currently running servlet?

user1608790
  • 393
  • 1
  • 3
  • 12
  • 3
    Which manifest attributes are you planning to read? Many of them are obtainable with existing Java SE methods. – VGR Nov 02 '15 at 15:39
  • 1
    VGR +1, getting version for example: String version = getClass().getPackage().getImplementationVersion(); – Rustam Nov 02 '15 at 16:03
  • 1
    Above comments are inapplicable for WAR's own manifest and therefore wrong. – BalusC Nov 02 '15 at 16:05
  • @BalusC You're right. I overlooked that he wants the .war file's manifest. The title probably should be edited to clarify that. – VGR Nov 02 '15 at 16:08
  • Servlets run per definition in WAR. Moreover, it's already tagged. – BalusC Nov 02 '15 at 16:08
  • 1
    Of course. But there can be a manifest in WEB-INF/classes and in each .jar under WEB-INF/lib. – VGR Nov 02 '15 at 16:10

1 Answers1

5

I also tried

this.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF");

but it returns a null.

That path must start with / in order to represent an absolute WAR resource path.

this.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");

Using ClassLoader#getResourceXxx() doesn't make sense as WAR's own manifest file isn't located in classpath. It's located in webroot, next to /WEB-INF and all. Therefore, ServletContext#getResourceXxx() is the only way.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This was helpful. I just don't understand how it is possible that reading all manifests by this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); did not fail as well. – user1608790 Nov 05 '15 at 12:59
  • 1
    You're welcome. Those come from classpath not from webcontent. Other rules apply. See also a.o. http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-properties-files-in-a-jsp-servlet-web-application/ – BalusC Nov 05 '15 at 13:00