我想以编程方式创建一个OSGi框架,使用它加载一个Bundle并从该Bundle中加载一个类。当我打电话给Bundle.loadClass()
时,我得到一个Class
,其中所有fields \ methods \ constructor字段都设为null。它只有一个名字。我无法访问任何公共方法等。我已经尝试了Equinox和Felix,结果相同。Bundle.loadClass()返回没有字段或方法的类实例
捆绑的MANIFEST
:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Activator: org.osgitest.osgitest.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Name: OSGi Test
Bundle-SymbolicName: org.osgitest.osgitest
Bundle-Version: 1.0
Import-Package: org.osgi.framework
Export-Package: org.osgitest.osgitest.test
框架设置:
FrameworkFactory ff = new EquinoxFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();
和
FrameworkFactory ff = new org.apache.felix.framework.FrameworkFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();
包加载:
Bundle testBundle = framework.getBundleContext().installBundle("file:C:\\org-osgitest-osgitest.jar");
testBundle.start();
Class<?> classOne = testBundle.loadClass("org.osgitest.osgitest.test.ClassOne");
Class<?> activator = testBundle.loadClass("org.osgitest.osgitest.Activator");
激活码Class
实例包含构造函数引用,但没有公共方法。它有public void start(BundleContext c)
和public void stop(BundleContext c)
。
如何加载正确的Class
?我究竟做错了什么?谢谢你的帮助。