2 Replies Latest reply on Nov 8, 2010 10:53 AM by alvescarlos

    How to read a MANIFEST.MF file from inside a jar?

    alvescarlos Newbie
      Hello,
      I have an application that shows its version number by reading some attributes saved inside the MANIFEST.MF file of the applications jars.
      After migrating to JBoss 5.1 this feature as stopped working, since everytime I try to read the file I get a
      {quote}java.io.FileNotFoundException: myfile.jar (The system cannot find the file specified){quote}
      This is the way I use to read the manifest attributes:
      {code}// Get the location of this class
      ProtectionDomain protectionDomain = clazz.getProtectionDomain();
      CodeSource codeSource = protectionDomain.getCodeSource();
      URL urlJar = codeSource.getLocation();
      String jarFilename = urlJar.getFile();
      // Replace all '%20' by ' '
      jarFilename = jarFilename.replaceAll("%20", " ");
      File file = new File(jarFilename);
      if (file.isFile()) {
      try {
      // Open the JAR file
      JarFile jarFile = new JarFile(file);
      // Get the manifest
      Manifest manifest = jarFile.getManifest();
      // Close the JAR file
      jarFile.close();
      // Get the manifest main attributes
      Attributes attribs = manifest.getMainAttributes();
      // Get attribute value
      myVersion = attribs.getValue("My-Version");
      myBuild = attribs.getValue("My-Build");
      } catch (IOException ex) {
      //output the exception to the console instead of hiding it
      ex.printStackTrace();
      myVersion = "N/A";
      myBuild = "N/A";
      }
      } else {
      myVersion = "N/A";
      myBuild = "N/A";
      }{code}
      I supose this as something to do with the new VFS implementation. Is this the right direction?
      Can someone please point me to how can I get to a jar file on this new JBoss protocol?
      Thank you very much in advance.
      Cheers!

      Hello,

       

      I have an application that shows its version number by reading some attributes saved inside the MANIFEST.MF file of the applications jars.

      After migrating to JBoss 5.1 this feature as stopped working, since everytime I try to read the file I get a

       

      {quote}java.io.FileNotFoundException: myfile.jar (The system cannot find the file specified){quote}

       

      This is the way I use to read the manifest attributes:

       

      {code:java}// Get the location of this class

      ProtectionDomain protectionDomain = clazz.getProtectionDomain();

      CodeSource codeSource = protectionDomain.getCodeSource();

      URL urlJar = codeSource.getLocation();

       

      String jarFilename = urlJar.getFile();

      // Replace all '%20' by ' '

      jarFilename = jarFilename.replaceAll("%20", " ");

       

      File file = new File(jarFilename);

       

      if (file.isFile()) {

      try {

      // Open the JAR file

      JarFile jarFile = new JarFile(file);

      // Get the manifest

      Manifest manifest = jarFile.getManifest();

      // Close the JAR file

      jarFile.close();

      // Get the manifest main attributes

      Attributes attribs = manifest.getMainAttributes();

      // Get attribute value

      myVersion = attribs.getValue("My-Version");

      myBuild = attribs.getValue("My-Build");

      } catch (IOException ex) {

      //output the exception to the console instead of hiding it

      ex.printStackTrace();

       

      myVersion = "N/A";

      myBuild = "N/A";

      }

      } else {

      myVersion = "N/A";

      myBuild = "N/A";

      }{code}

       

      I supose this as something to do with the new VFS implementation. Is this the right direction?

      Can someone please point me to how can I get to a jar file on this new JBoss protocol?

       

      Thank you very much in advance.

       

      Cheers!