2013-02-27 38 views
2

我尝试了多种方法从正确的jar中访问清单。 我的问题是它似乎在读取它所涉及的第一个jar文件,而不是我的课程。 这是我最新的尝试我无法在我的java代码中访问正确的MANIFEST.MF文件

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

而这些结果

测试= NULL 通过= 1.6.0_18(Sun微系统公司)

我我想要的MANIFEST.MF有值为测试,所以我知道这是读错文件

有谁知道我可以如何指定包含清单文件的jar?

回答

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 
} 
+0

不幸的是仍然给我同样的不正确的结果...... 测试= NULL 通过= 1.6.0_18(Sun微系统公司) – AttikAttak 2013-02-27 16:37:17

+0

他已经读文件,否则输出会一直:_NULL没有输入流_ – OscarRyz 2013-02-27 16:40:56

1

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

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

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

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

它与所有的路径操作和罐子名的硬编码非常脏...... 请问这件事情?再一次感谢你!