Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the manifest file for a webapp running in apache tomcat?

I have a webapp which contains a manifest file, in which I write the current version of my application during an ant build task. The manifest file is created correctly, but when I try to read it in during runtime, I get some strange side-effects. My code for reading in the manifest is something like this:

    InputStream manifestStream = Thread.currentThread()                                  .getContextClassLoader()                                  .getResourceAsStream("META-INFFFF/MANIFEST.MF");     try {         Manifest manifest = new Manifest(manifestStream);         Attributes attributes = manifest.getMainAttributes();         String impVersion = attributes.getValue("Implementation-Version");         mVersionString = impVersion;     }     catch(IOException ex) {         logger.warn("Error while reading version: " + ex.getMessage());     } 

When I attach eclipse to tomcat, I see that the above code works, but it seems to get a different manifest file than the one I expected, which I can tell because the ant version and build timestamp are both different. Then, I put "META-INFFFF" in there, and the above code still works! This means that I'm reading some other manifest, not mine. I also tried

this.getClass().getClassLoader().getResourceAsStream(...) 

But the result was the same. What's the proper way to read the manifest file from inside of a webapp running in tomcat?

Edit: Thanks for the suggestions so far. Also, I should note that I am running tomcat standalone; I launch it from the command line, and then attach to the running instance in Eclipse's debugger. That shouldn't make a difference, should it?

like image 387
Nik Reiman Avatar asked Mar 05 '09 16:03

Nik Reiman


People also ask

Where can I find manifest file in Java?

The manifest file is named MANIFEST. MF and is located under the META-INF directory in the JAR. It's simply a list of key and value pairs, called headers or attributes, grouped into sections.

Where is the manifest file?

The file is located at WorkspaceName>/temp/<AppName>/build/luaandroid/dist. The manifest file provides essential information about your app to the Android operating system, and Google Play store. The Android manifest file helps to declare the permissions that an app must have to access data from other apps.

What does manifest file contain?

The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

What is the task of manifest file in Java?

The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this "meta" information that the manifest contains, you enable the JAR file to serve a variety of purposes.


1 Answers

Maybe your side-effects come from the fact that almost all jars include a MANIFEST.MF and you're not getting the right one. To read the MANIFEST.MF from the webapp, I would say:

ServletContext application = getServletConfig().getServletContext(); InputStream inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF"); Manifest manifest = new Manifest(inputStream); 

Please note that running Tomcat from Eclipse is not the same as running Tomcat alone as Eclipse plays with the classloader.

like image 84
Pascal Thivent Avatar answered Sep 20 '22 14:09

Pascal Thivent