Access resources in another osgi bundle?

自作多情 提交于 2019-12-03 10:58:34

If you're writing an Eclipse plug-in you could try something like:

Bundle bundle = Platform.getBundle("your.plugin.id");

Path path = new Path("path/to/a/file.type");

URL fileURL = Platform.find(bundle, path);

InputStream in = fileURL.openStream();

The getEntry(String) method on Bundle is intended for this purpose. You can use it to load any resource from any bundle. Also see the methods findEntries() and getEntryPaths() if you don't know the exact path to the resource inside the bundle.

There is no need to get hold of the bundle's classloader to do this.

Have you considered adding a method to bundle A's API that loads and returns the resource?

Many might consider this a better design as it allows the name or means of storage of the resource to change without breaking clients of bundle A.

Have you tried using the BundleContext of bundle A to load resources?

Navigator Voyager

Try with /; if you don't put /, the class loader will try to load the resource from the same bundle.

this.getClass().getClassLoader().getResourceAsStream( "/bundle_A.properties")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!