OSGi类加载器找不到类ClassNotFoundException。

huangapple go评论69阅读模式
英文:

OSGi Classloader cant find class ClassNotFoundException

问题

我正在为一个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();
                }
            }
            
        }
    }
英文:

OSGi类加载器找不到类ClassNotFoundException。im programming an rcp application for a XML document.

Im trying to load my extensions without creating an object of it.

Sadly I get an ClassNotFoundException when I start the application.

I have no Idea why it dosent find my classes, do you guys have any idea how to fix this?

Part of my XML document. Labels are my classes:

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

Thats my Code for loading my classes :

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

答案1

得分: 1

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

因此
```java
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);

<details>
<summary>英文:</summary>

You need to get the value of the `class` attribute from the configuration element, load that class, and then access the constructor on that class.

So replace 

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


with

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

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

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


</details>



huangapple
  • 本文由 发表于 2020年9月2日 20:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定