我想以編程方式創建一個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
?我究竟做錯了什麼?謝謝你的幫助。