The problem is very likely that you try to work with bundles while you are not a bundle because you start the framework. This means your classloader is not an OSGi class loader but the normal Java application class loader. This loader will load the class for your cast operator while the bundle loads it through an OSGi class loader.
If you really need to make this work, export the package from the class path (set a property called org.osgi.framework.systempackages with the class names). Then the bundle will also load the class from the same source.
However, the way you use OSGi is for very special applications that want to embed a framework. These apps must be extremely careful with the classloading because nobody is paying attention. Class loading with OSGi is quite straightforward but if you don't play by its rules, you get this result :-)
Therfore, always test with bundle-bundle operations and not system-bundle.
Hope this helps, kind regards,
Peter Kriens
[QUOTE=alblue;363]
Quote:
Originally Posted by simongdkelly
It seems this isn't possible with Eclipse Equinox : EclipseZone - Programmatically start Equinox in ...
Urm. Why do you say that? The link you posted doesn't say anything about what you can/can't do.
It is possible to do this in Equinox; however, you need to be careful that the classes (particularly the interfaces) are ones that you have defined in your application. OSGi throws in a bunch of classloaders that need due and careful attention to expose them, so if you're not dealing with interfaces and implementations at the main Java level, it won't work.
You also assume, naively, that the name of the class is unique. It's not. You can have many classes of the same name loaded by many classloaders; and the classloader doesn't need to make that class visible to others. So the fact you're getting a 'ClassCastException' indicates that the class is not visible from your calling code, not that it doesn't work.
|