首页 文章

我无法在我的java代码中访问正确的MANIFEST.MF文件

提问于
浏览
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用于单元测试?

5 回答

  • 1

    这就是我想出来的.....注意用你的类/对象替换 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");
        }
    
  • 0

    你可以尝试这个:

    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
    }
    
  • 1

    您可以访问包含您的类的jar:

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

    我在你的代码中更改了这一行并得到了相同的结果,直到你找到清单文件然后从那里读取它 .

  • 2

    "Test"不是main attribute .

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

  • 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名称的硬编码都很脏......这有关系吗?再一次谢谢你!

相关问题