简体   繁体   中英

OSGi - Adding bundle's classes to specific classloader

I have bundle A.jar installed in OSGi and I can get this bundle using BundleContext

Bundle bundleA = context.getBundle()

And a specific classloader B

How can I adding all class from bundleA to my classloader B

You can not do this at runtime. You can add a Require-Bundle header to bundle B. Then the classloader of bundle B will know all classes of bundle A. This is not really recommended though.

So I would rather ask you what do you want to achieve with this? Perhaps there is another solution to your problem.

Let me assume you are writing your own class loader and want to use Bundle A's class loader as its parent. You can get the class loader for Bundle A as follows:

ClassLoader clA = bundleA.adapt(BundleWiring.class).getClassLoader();

You can then use clA as the parent of a new ClassLoader instance for loading your classes which depend upon classes from A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM