【问题标题】:I cannot access the correct MANIFEST.MF file in my java code我无法在我的 java 代码中访问正确的 MANIFEST.MF 文件
【发布时间】:2013-02-27 16:17:50
【问题描述】:

我尝试了多种方法来在单元测试中从正确的 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 以供单元测试使用?

【问题讨论】:

    标签: java jar manifest.mf


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      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
      }
      

      【讨论】:

      • 不幸的是,这仍然给我同样的错误结果... Test=null By=1.6.0_18 (Sun Microsystems Inc.)
      • 他已经读取了文件,否则输出会是:null no input stream
      【解决方案3】:

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

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

      我在您的代码中更改了这一行并得到了相同的结果,you might want to walk and read the whole jar your self 直到您找到清单文件然后从那里读取它。

      【讨论】:

        【解决方案4】:

        “测试” 不是 main attribute

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

        【讨论】:

          【解决方案5】:

          这是我最后用的方法……

          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 名称的硬编码都非常脏... 这有关系吗?再次感谢您!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-02-27
            • 2017-09-27
            • 2013-12-23
            • 2016-11-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多