2

I have tried numerous ways to access the manifest from the correct jar in a unit test. The issue I have is it seems to be reading the first jar file it comes to and not the one for my class. This is my latest attempt

InputStream is = Test.class.getResourceAsStream("/META-INF/MANIFEST.MF");
try {
    if (is == null) {
        System.out.println("null no input stream");
    } else {
        Manifest manifest = new Manifest(is);
        Attributes attributes = manifest.getMainAttributes();
        v = attributes.getValue("Test");
        System.out.println("Test="+v);
        v = attributes.getValue("Created-by");
        System.out.println("By="+v);
    }
} catch (IOException e) {
    System.out.println("error");
} finally {
    if (is != null) {
        is.close();
    }
}

And these are the results

Test=null By=1.6.0_18 (Sun Microsystems Inc.)

The MANIFEST.MF I want to use has a value for Test, so I know this is reading the wrong file

Does anyone know how I can specify the jar which contains the manifest file for the unit test to use?

5 Answers 5

2

This is what I came up with..... note replace HTMLDocument with your class/object.

   HTMLDocument htmlDoc = new HTMLDocument();

    try
    {
      JarFile jarfile = new JarFile(htmlDoc.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

      // Get the manifest
      Manifest manifest = jarfile.getManifest();

      Map<String,Attributes> msa = manifest.getEntries();

      for (String key : msa.keySet())
      {
        System.out.println("KEY: " + key);

        Attributes a = msa.get(key);

        for (Object k : a.keySet())
        {
          System.out.println(k + " - " + a.get(k));
        }
      }
    }
    catch (IOException e)
    {
      System.out.println("error");
    }
1

You can try with this:

URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader();
try {
  URL url = classLoader.findResource("META-INF/MANIFEST.MF");
  Manifest manifest = new Manifest(url.openStream());
  // add your code
  ...
} catch (IOException E) {
  // handle exception
}
2
  • unfortunately that is still giving me the same incorrect result ... Test=null By=1.6.0_18 (Sun Microsystems Inc.)
    – AttikAttak
    Feb 27, 2013 at 16:37
  • He already read the file, otherwise the output would've been: null no input stream
    – OscarRyz
    Feb 27, 2013 at 16:40
1

You can access the jar that contains your class with:

InputStream in = MyClass
                .class
                .getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .openStream();

I changed this line in your code and got the same results though, you might want to walk and read the whole jar your self until you find the manifest file and then read it from there.

0

"Test" is not a main attribute.

Try getEntries() and look up your custom attribute there.

0

This is the method i finally used ...

String path = Test.class.getProtectionDomain()
            .getCodeSource().getLocation().getPath();

    path = path.replace("classes/", "");

    URL url = null;
    try {
        url = new URL("jar:file:"+path+"test.jar!/");
        JarURLConnection jarConnection = null;
        try {
            jarConnection = (JarURLConnection)url.openConnection();
            Manifest manifest = jarConnection.getManifest();

            java.util.jar.Attributes manifestItem = manifest.getMainAttributes();
            System.out.println(manifestItem.getValue("Test"));
            System.out.println(manifestItem.getValue("Implementation-Version"));


        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

Its pretty dirty with all that path manipulation and hard coding of the jar name... Does this matter? thank you once again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.