Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading own MANIFEST.MF in Java servlet [duplicate]

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?

like image 392
user1608790 Avatar asked Nov 02 '15 15:11

user1608790


1 Answers

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.

like image 162
BalusC Avatar answered Nov 10 '22 04:11

BalusC