Link to home
Start Free TrialLog in
Avatar of kalyandm

asked on

read application.xml or manifest

Hi friends
I have an EAR file which has application.xml and 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  called "Implementation-Version".Please can anyone suggest how I can do this.

Avatar of mbvvsatish
Flag of Canada image

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

or here is an alternative approach

Avatar of kalyandm


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.
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
i will try to find out, but its not that easy. it will be good if you increase the points.
points increased!!
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.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{
          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]);
            java.util.jar.Manifest earManifest = new java.util.jar.Manifest(new FileInputStream(file.getAbsolutePath().toString()+"\\MANIFEST.MF"));
          Attributes attribMap = earManifest.getMainAttributes();

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

  public void archiveVersionInfo(File archive)throws Exception{
    JarFile earFile = new JarFile(archive, true, JarFile.OPEN_READ);
      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());

  public static final void main(String [] args){
      return ;
    String earFileName = args[0];
    File earFile = new File(earFileName);
    EarVersion versionChecker = new EarVersion();
      if (earFile.isDirectory()) {
      else {
    }catch(Exception ex){
      System.err.println("Error obtaining version for Enterprise Application Repository: "+ex.toString());
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..
I'm trying to use
   InputStream is  = this.getClass().getResourceAsStream ("meta-inf/");

to resolve the path of manifest file within ear but it returns null.Any ideas please?
try giving
Tried it doesn't work..sorry..
Basically the class in which check for manifest exists like this


so myclass should access manifest file.

Hi friends any idea please .do i need to modify the classpath or something in my manifest file under ear...
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.

can you please explain how you have solved the problem?
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
Avatar of Computer101
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial