【问题标题】:OSGi Classloader cant find class ClassNotFoundException [closed]OSGi Classloader 找不到类 ClassNotFoundException [关闭]
【发布时间】:2020-12-21 15:06:07
【问题描述】:

im 为 XML 文档编写一个 rcp 应用程序。

我正在尝试加载我的扩展而不创建它的对象。

很遗憾,当我启动应用程序时,我得到了一个 ClassNotFoundException

我不知道为什么它找不到我的课程,你们知道如何解决这个问题吗?

我的 XML 文档的一部分。标签是我的课程:

                        <Label>main</Label>
                        <Fields>
                            <Field id="main.text" type="text">
                                <Label>Flag</Label>
                            </Field>
                            <Field id="main.multilinetext" type="multilinetext">
                                <Label>Multiline Flag</Label>
                            </Field>
                            <Field id="main.negativnumber" type="number">
                                <Label>num</Label>
                            </Field>
                            <Field id="main.positivenumber" type="positivenumber">
                                <Label>positiv num</Label>
                            </Field>
                            <Field id="main.negativnumber" type="negativnumber">
                                <Label>negativ num</Label>
                            </Field>
                            <Field id="main.negativnumber" type="image">
                                <Label>img</Label>
                            </Field>
                        </Fields>
                    </Node>

这就是我加载课程的代码:

        public void execute() {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        
        IConfigurationElement[] config = registry.getConfigurationElementsFor(FIELDWIDGET_ID);
        for (IConfigurationElement configElement : config) { 
            if (configElement.getAttribute("type") != null) {
                System.out.println(configElement.getAttribute("type"));
                String pluginId = configElement.getContributor().getName();
                Bundle bundle = Platform.getBundle(pluginId);
                try {
                    Class<?> theClass = bundle.loadClass("class");
                    Constructor<?> con = configElement.getClass().getConstructor(Field.class, Composite.class);
                    hashMap.put(configElement.getAttribute("type"), con);
                } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e1) {
                    e1.printStackTrace();
                }
            }
            
        }
    }

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: java xml osgi classloader rcp


【解决方案1】:

您需要从配置元素中获取class 属性的值,加载该类,然后访问该类的构造函数。

所以替换

Class<?> theClass = bundle.loadClass("class");
Constructor<?> con = configElement.getClass().getConstructor(Field.class, Composite.class);

String className = configElement.getAttribute("class");

Class<?> theClass = bundle.loadClass(className);

Constructor<?> con = theClass.getConstructor(Field.class, Composite.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多