Avatar of kalyandm
kalyandm
 asked on

read application.xml or manifest

Hi friends
I have an EAR file which has application.xml and manifest.mf files.Now I have a java class which is alos part of the same EAR and has a method that needs to read a property from manifest.mf  called "Implementation-Version".Please can anyone suggest how I can do this.

Thanks
Java

Avatar of undefined
Last Comment
Computer101

8/22/2022 - Mon
mbvvsatish

you can use the Class java.util.jar.Manifest to read the manifest files

or here is an alternative approach
http://forum.java.sun.com/thread.jspa?threadID=642761&messageID=3887881

kalyandm

ASKER
Thanks .but I want to know How I can read the manifest from within ear for eg. manifest is located in meta-inf/ within ear >how can I get refrence that and then get it's attributes.
mbvvsatish

here is an example

http://blog.exadel.com/?p=7
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
kalyandm

ASKER
Hi
Thanks.I have seen thsi example already and we have to pass the name of the jar as an argument so it works fine but in my case I already have an ear and my class should find the manifest file within this ear and then read the properties from the manifest file.if you can help on this please
mbvvsatish

i will try to find out, but its not that easy. it will be good if you increase the points.
kalyandm

ASKER
points increased!!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mbvvsatish

following is the program that reads the manifest file in a ear file (Archive & Directory Format)

use like this
java EarVersion Your_EAR_Directory
java EarVersion YourEar.ear


import java.io.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Attributes;


public class EarVersion{

  private static final String ATTR_IMPL_VERSION="Implementation-Version";
  private static final String ATTR_IMPL_TITLE="Implementation-Title";
  private static final String ATTR_IMPL_VENDOR="Implementation-Vendor";


  private static void printUsage(){
    System.out.println("java -jar earversion.jar {EAR file|Directory}");
  }

  public void directoryVersionInfo(File directory)throws Exception{
    String [] archives = directory.list(new java.io.FilenameFilter(){
          public boolean accept(File dir, String name){

            boolean result = (name.endsWith(".jar")||name.endsWith(".war"))||new File(dir,name).isDirectory();

            return result;
          }
        });
    for(int i = 0;i<archives.length;i++){
      File file = new File(directory, archives[i]);
      if(file.isDirectory()){
        directoryVersionInfo(file);
            System.out.println(file.getAbsolutePath());
            java.util.jar.Manifest earManifest = new java.util.jar.Manifest(new FileInputStream(file.getAbsolutePath().toString()+"\\MANIFEST.MF"));
          Attributes attribMap = earManifest.getMainAttributes();
            displayVersionInfo(attribMap);
        }
    }
  }

  private static void displayVersionInfo(Attributes attribMap){
    String version = (String) attribMap.getValue(ATTR_IMPL_VERSION);
    if(version!=null){
      System.out.println("\tVersion: " + version);
    }
    String vendor = (String) attribMap.getValue(ATTR_IMPL_VENDOR);
    if(vendor!=null){
      System.out.println("\tVendor:  " + vendor);
    }
    String title = (String) attribMap.getValue(ATTR_IMPL_TITLE);
    if(title!=null){
      System.out.println("\tTitle:   " + title);
    }
    if(version==null&&title==null&vendor==null){
      System.out.println("\tNo version information available.");
    }
  }

  public void archiveVersionInfo(File archive)throws Exception{
    JarFile earFile = new JarFile(archive, true, JarFile.OPEN_READ);
    try{
      java.util.jar.Manifest earManifest = earFile.getManifest();
      if (earManifest == null) {
        throw new Exception("No Manifest file in " + archive.getName());
      }
      Attributes attribMap = earManifest.getMainAttributes();
      if (attribMap == null) {
        throw new Exception("Invalid Manifest file in " + archive.getName());
      }

      System.out.println("Hello/" +archive.getName());
      displayVersionInfo(attribMap);
    }finally{
      earFile.close();
    }
  }

  public static final void main(String [] args){
    if(args.length<1){
      printUsage();
      return ;
    }
    String earFileName = args[0];
    File earFile = new File(earFileName);
    EarVersion versionChecker = new EarVersion();
    try{
      if (earFile.isDirectory()) {
        versionChecker.directoryVersionInfo(earFile);
      }
      else {
        versionChecker.archiveVersionInfo(earFile);
      }
    }catch(Exception ex){
      System.err.println("Error obtaining version for Enterprise Application Repository: "+ex.toString());
      ex.printStackTrace();
    }
  }
}
kalyandm

ASKER
Hi
Thanks for the post.As I mentioned in my previous post, I don't have an option to specify the file name (of the ear) anywhere in my class as my class is the part of ear itself and it needs to refer to manifest within the same ear.sing fileinputstream is  a good option but how can I navigate with in the ear to find manifest file is the question..
kalyandm

ASKER
I'm trying to use
   InputStream is  = this.getClass().getResourceAsStream ("meta-inf/manifest.mf");

to resolve the path of manifest file within ear but it returns null.Any ideas please?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
mbvvsatish

try giving
../meta-inf/manifest.mf
or
../../meta-inf/manifest.mf
kalyandm

ASKER
Tried it doesn't work..sorry..
Basically the class in which check for manifest exists like this

x.ear--
         |__meta-inf/manifest.mf
         |__a.jar--
                       |_myclass

so myclass should access manifest file.



                   
kalyandm

ASKER
Hi friends any idea please .do i need to modify the classpath or something in my manifest file under ear...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kalyandm

ASKER
Hello Administrator,
Please can you close this question as I have found the soultion myself now.I just had to correct the path and it works.

Thanks
mbvvsatish

can you please explain how you have solved the problem?
kalyandm

ASKER
I did not manage to access application.xml/manifest file as such but we created a new file which just has the version number of build in same jar and I'm reading that.tha solves the problem but I'm still looking at how I can access manifest under the ear file.

Hope this clarifies
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Computer101

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question