1

I want to create an OSGi framework programmatically, load a Bundle with it and load a class from that bundle. When I call Bundle.loadClass() I get a Class isntance with all fields\methods\constructor fields set to null. It only has a name. I can't access any public methods, etc. I have tried both Equinox and Felix with the same result.

Bundle's 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

Framework setup:

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();

and

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 loading:

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");

Activator Class instance contains constructor reference, but no public methods. It has public void start(BundleContext c) and public void stop(BundleContext c).

How can I load the correct Class? What am I doing wrong? Thanks for any help.

4

2 回答 2

1

Class使用某种惰性初始化。Class只有在执行等之后,实例才会填充方法/字段getMethods。这就是为什么我在调试器中看不到任何东西的原因。

我试图调用public static void main(String[] args)并没有将我String[]的 to 转换为Object. 所以它被解释Object[]为 JVM 寻找带有签名的方法(args[0].class, args[1].class, ... , args[n].class)。这就是我的方法没有找到的原因。

于 2011-11-02T08:37:10.423 回答
0

由于捆绑包是一个 jar,您是否尝试过以正常的 -classpath 模式加载该类?OSGi 只是定义了类加载器。VM 仍然负责从字节码创建 Class 对象。所以我看不到 OSGi 框架如何从 Class 对象中“剥离”成员。

于 2011-10-12T18:20:25.460 回答