阅读我自己的 Jar 清单 [英] Reading my own Jar's Manifest

查看:25
本文介绍了阅读我自己的 Jar 清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读 Manifest 文件,它提供了我的课程,但是当我使用:

I need to read the Manifest file, which delivered my class, but when I use:

getClass().getClassLoader().getResources(...)

我从加载到 Java 运行时的第一个 .jar 中获取了 MANIFEST.
我的应用程序将从小程序或 webstart 运行,
所以我想我将无法访问我自己的 .jar 文件.

I get the MANIFEST from the first .jar loaded into the Java Runtime.
My app will be running from an applet or a webstart,
so I will not have access to my own .jar file, I guess.

我实际上想从启动的 .jar 中读取 Export-package 属性Felix OSGi,所以我可以将这些包公开给 Felix.有什么想法吗?

I actually want to read the Export-package attribute from the .jar which started the Felix OSGi, so I can expose those packages to Felix. Any ideas?

推荐答案

您可以做以下两件事之一:

You can do one of two things:

  1. 调用 getResources() 并遍历返回的 URL 集合,将它们作为清单读取,直到找到您的:

  1. Call getResources() and iterate through the returned collection of URLs, reading them as manifests until you find yours:

Enumeration<URL> resources = getClass().getClassLoader()
  .getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
    try {
      Manifest manifest = new Manifest(resources.nextElement().openStream());
      // check that this is your manifest and do what you need or get the next one
      ...
    } catch (IOException E) {
      // handle
    }
}

  • 您可以尝试检查getClass().getClassLoader() 是否是java.net.URLClassLoader 的实例.大多数 Sun 类加载器都是,包括 AppletClassLoader.然后你可以投射它并调用已知的 findResource() - 至少对于小程序 - 直接返回所需的清单:

  • You can try checking whether getClass().getClassLoader() is an instance of java.net.URLClassLoader. Majority of Sun classloaders are, including AppletClassLoader. You can then cast it and call findResource() which has been known - for applets, at least - to return the needed manifest directly:

    URLClassLoader cl = (URLClassLoader) getClass().getClassLoader();
    try {
      URL url = cl.findResource("META-INF/MANIFEST.MF");
      Manifest manifest = new Manifest(url.openStream());
      // do stuff with it
      ...
    } catch (IOException E) {
      // handle
    }
    

  • 这篇关于阅读我自己的 Jar 清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆