2

我尝试了多种方法来在单元测试中从正确的 jar 访问清单。我遇到的问题是它似乎是在读取它的第一个 jar 文件,而不是我班级的那个。这是我最近的尝试

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();
    }
}

这些是结果

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

MANIFEST.MF我想使用的值为,Test所以我知道这是读取错误的文件

有谁知道我如何指定包含清单文件以供单元测试使用的 jar?

4

5 回答 5

2

这就是我想出的......注意HTMLDocumen用你的类/对象替换 t 。

   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");
    }
于 2013-02-27T18:48:39.867 回答
1

你可以试试这个:

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
}
于 2013-02-27T16:29:30.267 回答
1

您可以通过以下方式访问包含您的类的 jar:

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

我在您的代码中更改了这一行并得到了相同的结果,您可能想自己步行并阅读整个 jar,直到找到清单文件然后从那里读取它。

于 2013-02-27T17:18:39.073 回答
0

“测试”不是主要属性

尝试getEntries()并在那里查找您的自定义属性。

于 2013-02-27T16:40:23.910 回答
0

这是我最后使用的方法...

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.
    }

它对所有路径操作和 jar 名称的硬编码非常脏……这有关系吗?再一次感谢你!

于 2013-02-28T19:02:04.587 回答