XStream noClassDefFound 错误取决于运行时

Posted

技术标签:

【中文标题】XStream noClassDefFound 错误取决于运行时【英文标题】:XStream noClassDefFound error depending on runtime 【发布时间】:2021-02-28 15:19:01 【问题描述】:

我有点卡在 xstream 的问题上。我有两个基于 OSGI 的 RCP 应用程序。他们使用 xstream 在 XML 文件中写入和读取某些类。当我运行程序并触发有问题的代码时,我得到:

com.thoughtworks.xstream.mapper.CannotResolveClassException: at.jku.mevss.memodel.internal.meta.ImplMevolMetaModel
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.loadUncompressedStream(MEMetaModelXStreamPersistor.java:56)
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.open(MEMetaModelXStreamPersistor.java:44)
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.open(MEMetaModelXStreamPersistor.java:1)
        at at.jku.mevss.util.persistence.AbstractModelPersistenceProvider.getModel(AbstractModelPersistenceProvider.java:111)
        at at.jku.mevss.util.persistence.AbstractModelPersistenceProvider.getModel(AbstractModelPersistenceProvider.java:70)
        at at.jku.mevss.eventdistributor.server.metamodel.MetaModelServiceInstance.fromFile(MetaModelServiceInstance.java:100)
        at at.jku.mevss.eventdistributor.server.metamodel.MetaModelServiceInstance.fromFile(MetaModelServiceInstance.java:1)
        at at.jku.mevss.eventdistributor.service.core.service.AbstractFileTransmitServiceInstance.reloadItems(AbstractFileTransmitServiceInstance.java:95)
        at at.jku.mevss.eventdistributor.service.core.service.AbstractFileTransmitServiceInstance.notifyFileChange(AbstractFileTransmitServiceInstance.java:61)
        at at.jku.mevss.eventdistributor.service.core.util.WatchServiceRunner$NotifyChangeTask.run(WatchServiceRunner.java:107)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: at.jku.mevss.memodel.internal.meta.ImplMevolMetaModel
        at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
        at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47)
        at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
        at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1404)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1383)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1268)
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.loadUncompressedStream(MEMetaModelXStreamPersistor.java:53)
        ... 11 more

com.thoughtworks.xstream.mapper.CannotResolveClassException: at.jku.mevss.memodel.internal.meta.ImplMevolMetaModel
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.loadUncompressedStream(MEMetaModelXStreamPersistor.java:56)
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.open(MEMetaModelXStreamPersistor.java:44)
        at at.jku.mevss.memodel.internal.persistence.MEMetaModelXStreamPersistor.open(MEMetaModelXStreamPersistor.java:1)
        at at.jku.mevss.util.persistence.AbstractModelPersistenceProvider.getModel(AbstractModelPersistenceProvider.java:111)
        at at.jku.mevss.idetools.metaconfeditor.metamodel.editor.ModelEditor.doLoadItem(ModelEditor.java:253)
        at at.jku.mevss.idetools.metaconfeditor.metamodel.editor.ModelEditor.doLoadItem(ModelEditor.java:1)
        at at.jku.mevss.idetools.launch.editor.AbstractRMMItemEditor.setInput(AbstractRMMItemEditor.java:83)
        at org.eclipse.ui.forms.editor.FormEditor.init(FormEditor.java:128)
        at org.eclipse.ui.internal.EditorManager.createSite(EditorManager.java:828)
        at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:647)
        at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:465)
        at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
        at org.eclipse.ui.internal.PartPane.setVisible(PartPane.java:315)
        at org.eclipse.ui.internal.presentations.PresentablePart.setVisible(PresentablePart.java:180)
        at org.eclipse.ui.internal.presentations.util.PresentablePartFolder.select(PresentablePartFolder.java:270)
        at org.eclipse.ui.internal.presentations.util.LeftToRightTabOrder.select(LeftToRightTabOrder.java:65)
        at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation.selectPart(TabbedStackPresentation.java:473)
        at org.eclipse.ui.internal.PartStack.refreshPresentationSelection(PartStack.java:1245)
        at org.eclipse.ui.internal.PartStack.setSelection(PartStack.java:1198)
        at org.eclipse.ui.internal.PartStack.showPart(PartStack.java:1597)
        at org.eclipse.ui.internal.PartStack.add(PartStack.java:493)
        at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:103)
        at org.eclipse.ui.internal.PartStack.add(PartStack.java:479)
        at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:112)
        at org.eclipse.ui.internal.EditorSashContainer.addEditor(EditorSashContainer.java:63)
        at org.eclipse.ui.internal.EditorAreaHelper.addToLayout(EditorAreaHelper.java:225)
        at org.eclipse.ui.internal.EditorAreaHelper.addEditor(EditorAreaHelper.java:213)
        at org.eclipse.ui.internal.EditorManager.createEditorTab(EditorManager.java:808)
        at org.eclipse.ui.internal.EditorManager.openEditorFromDescriptor(EditorManager.java:707)
        at org.eclipse.ui.internal.EditorManager.openEditor(EditorManager.java:666)
        at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2946)
        at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2854)
        at org.eclipse.ui.internal.WorkbenchPage.access$11(WorkbenchPage.java:2846)
        at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:2797)
        at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
        at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2793)
        at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2777)
        at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2760)
        at org.eclipse.ui.ide.IDE.openEditor(IDE.java:687)
        at at.jku.mevss.ui.ide.EditorUtil$1.run(EditorUtil.java:25)
        at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
        at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
        at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4144)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
        at org.eclipse.jface.operation.ModalContext$ModalContextThread.block(ModalContext.java:173)
        at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:388)
        at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:507)
        at at.jku.mevss.ui.util.dialogs.AbstractUIRunnable.runRunnable(AbstractUIRunnable.java:116)
        at at.jku.mevss.idetools.metaconfeditor.handler.CreateNewMetamodelHandler.execute(CreateNewMetamodelHandler.java:13)
        at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290)
        at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
        at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
        at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
        at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:829)
        at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:815)
        at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:805)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
        at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
        at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
        at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
        at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
        at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
        at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
        at at.jku.mevss.product.application.monitoringmanager.MonitoringManagerApplication.start(MonitoringManagerApplication.java:25)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1438)

有问题的类at.jku.mevss.memodel.internal.meta.ImplMevolMetaModel在执行序列化和反序列化的同一个包中。虽然它在不同的包装中。在 IDE 中,这两个应用程序都可以正常工作,并且无需为 xstream 设置特定的类加载器。 我还尝试通过执行更改 xstream 使用的类加载器:

xstream.setClassLoader(Thread.currentThread().getContextClassLoader());

对于适用于构建版本的 2 个应用程序之一,它能够从 XML 读取和生成对象,但另一个应用程序似乎有不同的 ContextClassLoader,或者它没有相关类可加载类的列表。

我如何提供一个能够独立于运行 OSGI 包的应用程序加载给定类的类加载器?

【问题讨论】:

【参考方案1】:

通过将 xstream 的类加载器设置为 xstream.setClassLoader(getClass().getClassLoader());,我找到了解决问题的方法。 这对我来说仍然有点神秘,因为我已经查找了 xstream 的源代码,并且默认情况下 xstream 创建了一个复合类加载器类,它总是应该包含getClass().getClassLoader()Object.class.getClassLoader()。查看来源here。 所以最后基本上只是有点运气,我通过手动正确设置类加载器来仔细检查它。

【讨论】:

以上是关于XStream noClassDefFound 错误取决于运行时的主要内容,如果未能解决你的问题,请参考以下文章

java报错汇总

当我尝试保存测试计划时,为啥会收到 NoClassDefFound 错误?

UCanAccess 出现 ClassNotFoundException 和 NoClassDefFound 错误

使用gradle的NoClassDefFound异常

jmeter导入jmx文件报错:missing class com.thoughtworks.xstream.converters.ConversionException

Android 应用程序在 SDK-tools 更新版本后崩溃(NoClassDefFound,工具版本 22)