在 ExtensionPoint 的贡献中加载 class

Loading a class in contribution of an ExtensionPoint

我必须从我创建的扩展点中检索 class。我的目标是使用外部导出插件从我的 UI 插件导出文件。

我的服务 class Export 无法加载 ExportXML class 我不知道为什么。我检查了依赖关系,Bundle-Path,也在 build.properties 文件中添加了我的 org.jdom 依赖关系。但是,我仍然不知道,因为每次我尝试启动导出时,它都无法加载 class 并出现错误:

org.eclipse.core.runtime.CoreException: Plugin C was unable to load class C.ExportXML at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:194) at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:176) [...] Caused by: java.lang.ClassNotFoundException: C.ExportXML cannot be found by C_1.0.0.qualifier at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:448) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:361)

我有点无语,因为我找不到错误的来源。 我用来在插件 B 中检索 ExportXML class 的函数是:

IExport exportClass = null;
for(IConfigurationElement contribution : this.getContributions()){
    if(contribution.getAttribute(Export_ExtensionPoint_Attribute_formatExtension).equals(formatExtension)){
        Object o;
        try {
            o = contribution.createExecutableExtension(Export_ExtensionPoint_Attribute_exportClass);
        if(o instanceof IExport)
            exportClass = (IExport) o;
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }
}

这里是C plugin.xml :

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="C"
         point="B">
      <exportFormat
            ExportClass="C.ExportXML"
            formatExtension="xml"
            formatName="XML">
      </exportFormat>
   </extension>

</plugin>

这是我的 ExportXML header 在 C:

package C;
public class ExportXML implements IExport {
...
}

注意:我在try中输入时可以获得属性formatExtension,只是creatExecutableExtension方法失败了。

build.properties 文件:

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               plugin.xml,\
               icons/,\
               lib/jdom-2.0.6.jar

MANIFEST.MF :

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: XML Export
Bundle-SymbolicName: fr.enseeiht.waldo.tools.export.xml;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: fr.enseeiht.waldo.tools.export.xml.Activator
Bundle-Vendor: Waldo
Require-Bundle: org.eclipse.core.runtime,
 fr.enseeiht.waldo.tools.analyzer;bundle-version="2.0.0",
 fr.enseeiht.waldo.tools.export;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy

插件结构: https://i.stack.imgur.com/6EeI6.png

提前感谢您的帮助

您的 MANIFEST.MF 中没有 Bundle-ClassPath,这是告诉 Eclipse 在何处查找目标代码所必需的。它应该是这样的:

Bundle-ClassPath: .,
   lib/jdom-2.0.6.jar

在 MANIFEST.MF 编辑器中,您可以在 'Classpath' 部分的 'Runtime' 选项卡上进行设置。