读我自己的Jar's Manifest [英] Reading my own Jar's Manifest
问题描述
我需要阅读 Manifest
文件,该文件提供了我的课程,但是当我使用时:
I need to read the Manifest
file, which delivered my class, but when I use:
getClass().getClassLoader().getResources(...)
我从加载到Java运行时的第一个 .jar
中得到 MANIFEST
。
My应用程序将从applet或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 $ c $中读取
Export-package
属性c>启动了
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:
-
调用
getResources()
并遍历返回的URL集合,将其作为清单读取,直至找到您的:
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's Manifest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!