英文:
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();
}
}
}
}
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论