Java OSGi类加载器找不到类ClassNotFoundException
im为XML文档编写rcp应用程序 我试图加载我的扩展而不创建它的对象 遗憾的是,当我启动应用程序时,我得到了一个ClassNotFoundException 我不知道为什么找不到我的课,你们知道怎么解决吗 我的XML文档的一部分。标签是我的类:Java OSGi类加载器找不到类ClassNotFoundException,java,xml,osgi,classloader,rcp,Java,Xml,Osgi,Classloader,Rcp,im为XML文档编写rcp应用程序 我试图加载我的扩展而不创建它的对象 遗憾的是,当我启动应用程序时,我得到了一个ClassNotFoundException 我不知道为什么找不到我的课,你们知道怎么解决吗 我的XML文档的一部分。标签是我的类: <Label>main</Label> <Fields> <
<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>
main
旗帜
多行标志
号码
正数
负数
img
这是我加载类的代码:
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();
}
}
}
}
public void execute(){
IExtensionRegistry=Platform.getExtensionRegistry();
IConfigurationElement[]config=registry.getConfigurationElementsFor(FIELDWIDGET_ID);
对于(IConfigurationElement-configElement:config){
if(configElement.getAttribute(“type”)!=null){
System.out.println(configElement.getAttribute(“类型”);
字符串pluginId=configElement.getContributor().getName();
Bundle=Platform.getBundle(pluginId);
试一试{
类=bundle.loadClass(“类”);
构造函数con=configElement.getClass().getConstructor(Field.class,Composite.class);
put(configElement.getAttribute(“类型”),con);
}catch(NoSuchMethodException | SecurityException | ClassNotFoundException e1){
e1.printStackTrace();
}
}
}
}
您需要从配置元素中获取class
属性的值,加载该类,然后访问该类上的构造函数
因此,替换
Class<?> theClass = bundle.loadClass("class");
Constructor<?> con = configElement.getClass().getConstructor(Field.class, Composite.class);
Class theClass=bundle.loadClass(“类”);
构造函数con=configElement.getClass().getConstructor(Field.class,Composite.class);
与
String className=configElement.getAttribute(“类”);
Class theClass=bundle.loadClass(类名);
构造函数con=class.getConstructor(Field.class,Composite.class);
请不要破坏您的帖子,为他人做更多的工作。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非破坏版本是分发的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:
String className = configElement.getAttribute("class");
Class<?> theClass = bundle.loadClass(className);
Constructor<?> con = theClass.getConstructor(Field.class, Composite.class);