gpt4 book ai didi

java - 主插件找不到在另一个插件中定义的类 - Eclipse 产品

转载 作者:行者123 更新时间:2023-11-30 09:09:25 26 4
gpt4 key购买 nike

我将我的插件项目导出为产品,当我运行该产品(eclipse 应用程序)时,主插件 ( org.example.framework.core ) 找不到在另一个插件 ( org.example.abc ) 中定义的类,该插件实现了对扩展的扩展点由主插件提供。该类是扩展中定义的元素之一。但是,当我在 Eclipse 中运行该项目时,一切运行正常!

这是日志( atvste.ppt.ptfwDescription.abc.decoderInfoorg.example.abc plugin 中的一个包):

0 [Worker-2] ERROR org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo.createInstance(MsgDecoderInfo.java:114) : can not create class for :atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC cannot be found by org.example.framework.core_1.0.0.201404111439
java.lang.ClassNotFoundException: atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC cannot be found by org.example.framework.core_1.0.0.201404111439
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo.createInstance(MsgDecoderInfo.java:104)
at org.example.framework.core.ptfw.codec.decode.DecoderInfo.<init>(DecoderInfo.java:56)
at org.example.framework.core.ptfw.codec.decode.DecoderInfo.createInstance(DecoderInfo.java:125)
at org.example.framework.core.ptfw.codec.ptfwObjectsFactory.decoderInfoItf_createInstance(ptfwObjectsFactory.java:200)
at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.createptfwDescription(ptfwDescriptionPersistenceJaxb.java:326)
at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.fillptfwDescription(ptfwDescriptionPersistenceJaxb.java:247)
at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.createInstance(ptfwDescriptionPersistenceJaxb.java:232)
at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.open(ptfwDescriptionPersistenceJaxb.java:146)
at org.example.framework.core.ptfw.codec.ptfwDescription.createInstance(ptfwDescription.java:152)
at org.example.framework.core.ptfw.codec.command.CmdLoadptfwDescription.loadptfwDescription(CmdLoadptfwDescription.java:50)
at org.example.framework.core.ptfw.codec.command.CmdLoadptfwDescription.execute(CmdLoadptfwDescription.java:40)
at org.example.framework.core.runtime.JobService$2.run(JobService.java:93)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

编辑:创建类实例的函数未找到

 public static IMessageDecoderInfo createInstance(XmlgMsgDecoderInfo pMsgDecoderInfoType,
IMessageDecoderInfo msgDecoder)
{

String className = pMsgDecoderInfoType.getClassName();
if(className!=null)
{
try
{
Class<?> formalArgs[] = new Class[1];
formalArgs[0] = XmlgMsgDecoderInfo.class;
Class<?> clazz;
if (msgDecoder != null)
{
clazz = msgDecoder.getClass();
}
else
{
clazz = Class.forName( className );
}
Constructor<?> constructor = clazz.getConstructor(formalArgs);
java.lang.Object actualArgs[] =
{ pMsgDecoderInfoType };

return (IMessageDecoderInfo)constructor.newInstance(actualArgs);
}catch(Exception e) {
String error = "can not create class for :" +className+ " " + e.getMessage();
if (LOGGER.isEnabledFor(Level.ERROR)) {
LOGGER.error(error, e);
}
throw new CreatePtfwElementRuntimeException(error, e);
}
}
return new MsgDecoderInfo(pMsgDecoderInfoType);
}`

最佳答案

由于 Eclipse 使用复杂的类加载器系统,您不能使用 Class.forName 加载另一个插件中的类。

如果您的代码正在处理扩展点定义,您将拥有用于指定类名的配置元素的 IConfigurationElement。你可以使用

IConfigurationElement configElement = ....;

Object classInstance = configElement.createExecutableExtension("class attribute name");

其中“类属性名称”是扩展点元素中指定要加载的类的属性名称。正在创建的类必须具有无参数构造函数。

或者,您可以使用以下方式加载类:

Bundle bundle = Platform.getBundle("plugin id");

Class<?> theClass = bundle.loadClass("class name");

... construct as usual ...

如果您有 IConfigurationElement,您可以使用以下方法获取插件 ID

String pluginId = configElement.getContributor().getName();

关于java - 主插件找不到在另一个插件中定义的类 - Eclipse 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23062874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com