我有插件A依赖于插件B,我试图从插件A加载插件B中定义的类。我确信该类在导出的包中。但是我得到了ClassNotFoundException,我没有错过任何东西吗?
page = (IPage) Class.forName(pageClass).newInstance();
错误消息:
java.lang.ClassNotFoundException: amarsoft.dbmp.credit.web.dataobject.DOListPage
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at amarsoft.rcp.base.widgets.DefaultPageRegistry.findPage(DefaultPageRegistry.java:27)
at amarsoft.rcp.base.widgets.AbstractPageControler.showPage(AbstractPageControler.java:27)
at amarsoft.rcp.base.widgets.AbstractPageControler.showPage(AbstractPageControler.java:37)
at amarsoft.dbmp.function.ui.FunctionListPage$3.linkActivated(FunctionListPage.java:261)
at org.eclipse.ui.forms.widgets.AbstractHyperlink.handleActivate(AbstractHyperlink.java:233)
at org.eclipse.ui.forms.widgets.AbstractHyperlink.handleMouseUp(AbstractHyperlink.java:327)
at org.eclipse.ui.forms.widgets.AbstractHyperlink.access$2(AbstractHyperlink.java:311)
at org.eclipse.ui.forms.widgets.AbstractHyperlink$4.handleEvent(AbstractHyperlink.java:125)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at amarsoft.adt.rcp.ADTApplication.start(ADTApplication.java:29)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
答案 0 :(得分:1)
如果你需要捆绑;来自pluginA的pluginB,然后Class.forName(*)
应该有效。虽然我会指出使用PageClass class = new PageClass();
也可以。当你可以直接依赖那个类时,为什么要尝试使用反射呢?
对于提供其他插件应通过提供实现扩展的框架行为的常见模式,eclipse提供了扩展点/扩展的概念。
插件A将提供扩展点。插件B将列出该扩展点的扩展,并提供实现类。在这种情况下,插件B需要插件A,而不是相反。
然后插件A使用org.eclipse.core.runtime.IExtensionRegistry.getConfigurationElementsFor(String)
读取所有扩展贡献和org.eclipse.core.runtime.IConfigurationElement.createExecutableExtension(String)
来实例化提供的实现(注册表负责确保可以实例化类)。