2

I'm trying to access the MANIFEST.MF file in my java application.

I'm doing the following

   //prints /usr/local/apache-tomcat-8.0.9/webapps/MyProject/WEB-INF/lib/activation-1.1.jar!/META-INF/MANIFEST.MF
    System.out.println( "We are looking at "+this.getClass().getClassLoader()
                        .getResource("META-INF/MANIFEST.MF"));` 
//Im looking to print  /usr/local/apache-tomcat-8.0.9/webapps/MyProject/META-INF/MANIFEST.MF


//prints-/usr/local/apache-tomcat-8.0.9/lib/
System.out.println("The base path is"+this.getClass().getClassLoader()
                        .getResource("."));

Here's the directory structure

MyProject
-src/main/java/my/packages/MyClass.java
-target
   -MyProject.war

Any thoughts on how I can correct my code would be great.

Directory structure of my war

Myproject
META-INF            apidocs             other folders
WEB-INF             
userNotFound
  • 347
  • 1
  • 4
  • 13

1 Answers1

0

You need to use the ServletContext.getResourceAsStream

I have found the classloader method not as reliable for MANIFEST files on web apps.

Also it's not going to reliably work well if your running your app from maven or eclipse wtp if your manifest is generated by your build process.

The real duplicate of the question: How do I read the manifest file for a webapp running in apache tomcat?

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Do you have a code snippet to back this up? I'm not using a web app. Would I have access to the ServletContext? – userNotFound Oct 24 '14 at 21:00
  • Why do you have tomcat paths in your examples? I'll post code later (on phone currently). – Adam Gent Oct 24 '14 at 21:02
  • thats the result of `this.getClass().getClassLoader()` which *should* return the absolute path to the current class – userNotFound Oct 24 '14 at 21:06
  • My manifest is auto generated by maven. So what options do I have? – userNotFound Oct 24 '14 at 21:20
  • How are you executing the project? It still looks like tomcat is running your code. Btw when I mean webapp I mean your code is running in a servlet container like tomcat even if it does no webapp like behavior. – Adam Gent Oct 24 '14 at 21:38
  • :Actually this **seems** to be working atm. Is it production worthy? **EDIT** In response to your comment. Yes, I was able to gain access to servletcontext from a web service layer. – userNotFound Oct 24 '14 at 21:45
  • I'm testing/executing this piece of code using POSTMAN. – userNotFound Oct 24 '14 at 22:06