在另一台计算机上运行应用程序时出现InvalidClassException

时间:2014-10-19 04:22:27

标签: java swing

这是在我在另一台计算机上打开项目后发生的,登录屏幕后出现此错误。  我在我的桌面计算机中包含了我用来创建它的所有库,它可以正常工作。  我找不到错误并修复它

这是第180到185行的代码,在错误中引用:

CB_vendor(javax.swing.JComboBox)java.beans.Beans.instantiate(getClass().getClassLoader(), "newproject.NewJFrame_CB_vendor");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (java.io.IOException e) {
    e.printStackTrace();
} ##

将项目导入NetBeans后,我以正常方式运行它:project ---right click -----run

完整错误:

java.io.InvalidClassException: javax.swing.JComboBox; local class incompatible: stream classdesc serialVersionUID = -1692960661481242205, local class serialVersionUID = 8351908727136020938
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at java.beans.Beans.instantiate(Beans.java:199)
    at java.beans.Beans.instantiate(Beans.java:80)
    at newproject.NewJFrame.initComponents(NewJFrame.java:180)
    at newproject.NewJFrame.<init>(NewJFrame.java:48)
    at newproject.logIn.jButton1ActionPerformed(logIn.java:241)
    at newproject.logIn.access$300(logIn.java:26)
    at newproject.logIn$4.actionPerformed(logIn.java:118)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.io.InvalidClassException: javax.swing.JComboBox; local class incompatible: stream classdesc serialVersionUID = -1692960661481242205, local class serialVersionUID = 8351908727136020938
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at java.beans.Beans.instantiate(Beans.java:199)
    at java.beans.Beans.instantiate(Beans.java:80)
    at newproject.NewJFrame.initComponents(NewJFrame.java:209)
    at newproject.NewJFrame.<init>(NewJFrame.java:48)
    at newproject.logIn.jButton1ActionPerformed(logIn.java:241)
    at newproject.logIn.access$300(logIn.java:26)
    at newproject.logIn$4.actionPerformed(logIn.java:118)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 27 seconds)

2 个答案:

答案 0 :(得分:5)

看起来你已经序列化了一个JComboBox,但是serialVersionUID在运行时知道的类和被序列化的类之间并不匹配。如果查看Javadoc for the Serializable interface,它会解释如何使用该字段来确保序列化版本以及JVM想要从序列化对象创建的内容是相同的。

由于该类是Java标准库的一部分,我建议您运行在一个JVM版本上序列化JComboBox的代码,并尝试在另一个版本上反序列化它。

作为旁注,序列化像JComboBox这样的UI元素很少是合适的。通常,您希望序列化数据,而不是显示数据的任何内容(因此您保存JComboBox的状态 - 选择的内容 - 然后将其还原到新的JComboBox中)。这样你就不会遇到这样的问题。

答案 1 :(得分:4)

您已在JRE版本中序列化JComboBox。来自Javadoc

  

警告:此类的序列化对象与以后的Swing版本不兼容。当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。