ClassNotFound反序列化序列化对象时的异常

时间:2015-09-12 22:22:13

标签: java object exception serialization classnotfoundexception

我试图序列化和反序列化一个对象。该对象可以包含对其他对象以及ArrayList和HashMap的引用。

当我尝试执行代码时,序列化工作正常,但反序列化却没有。它会导致以下异常:

Exception in thread "main" java.lang.ClassNotFoundException: experiment.Experiment$1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at javax.crypto.extObjectInputStream.resolveClass(SealedObject.java:490)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1613)
    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.defaultReadFields(ObjectInputStream.java:2000)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at javax.crypto.SealedObject.getObject(SealedObject.java:302)
    at experiment.Experiment.main(Experiment.java:52)
Java Result: 1

我有主要课程,实验,如下:

public class Experiment {  
    public static void main(String[] args) throws Exception {
        File data = new File("C:\\Users\\Furze\\Desktop\\experiment.dat");   
        // I only execute the following to encrypt the file, which works fine:
        Test test = new Test(new VariableMap<String, String>() {{
            put("Name", "Furze");
        }});   
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[] {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}, "Blowfish"));
        SealedObject sealedObject = new SealedObject(test, cipher);
        CipherOutputStream outputStream = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(data.getPath())), cipher);
        ObjectOutputStream objectOutput = new ObjectOutputStream(outputStream);
        objectOutput.writeObject(sealedObject);     
        objectOutput.close();
        // I then comment out the above code to test the file, which fails.
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new byte[] {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}, "Blowfish"));
        CipherInputStream inputStream = new CipherInputStream(new BufferedInputStream(new FileInputStream(data.getPath())), cipher);
        ObjectInputStream objectInput = new ObjectInputStream(inputStream);
        SealedObject sealedObject = (SealedObject) objectInput.readObject();
        Test test = (Test) sealedObject.getObject(cipher);
        System.out.println(test.variables.get("Name"));
    }
}

奇怪的是,如果我在回读时保留Test test = new Test(...);部分完整,但将名称更改为test_old之类的内容,则它似乎正常工作。

对象类如下:

// The VariableMap class is something I added during debugging to test if HashMap simply isn't serializable. It didn't help. It does have to stay a HashMap (or VariableMap!) however, for my code to operate correctly.
class VariableMap<Name, Value> extends HashMap<String, String> implements java.io.Serializable {
    public VariableMap() {
        super();
    }
}

public class Test implements java.io.Serializable {
    VariableMap<String, String> variables = new VariableMap<>();
    public Test() {}
    public Test(VariableMap<String, String> variables) {
        this.variables = variables;
    }
}

有人可以解释我的代码可能出现什么问题吗?我已经阅读了有关更改CLASSPATH的内容,但是当我尝试它时没有任何区别。

2 个答案:

答案 0 :(得分:2)

您在课堂上缺少包装声明,应该是:

package experiment;

public class Experiment implements Serializable {
    public static void main(String[] args) throws Exception {
        File data = new File("C:\\Users\\Furze\\Desktop\\experiment.dat");   
        // I only execute the following to encrypt the file, which works fine:
        Test test = new Test(new VariableMap<String, String>() {{
            put("Name", "Furze");
        }});   
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[] {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}, "Blowfish"));
        SealedObject sealedObject = new SealedObject(test, cipher);
        CipherOutputStream outputStream = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(data.getPath())), cipher);
        ObjectOutputStream objectOutput = new ObjectOutputStream(outputStream);
        objectOutput.writeObject(sealedObject);     
        objectOutput.close();
        // I then comment out the above code to test the file, which fails.
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new byte[] {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}, "Blowfish"));
        CipherInputStream inputStream = new CipherInputStream(new BufferedInputStream(new FileInputStream(data.getPath())), cipher);
        ObjectInputStream objectInput = new ObjectInputStream(inputStream);
        SealedObject sealedObject = (SealedObject) objectInput.readObject();
        Test test = (Test) sealedObject.getObject(cipher);
        System.out.println(test.variables.get("Name"));
    }
}

答案 1 :(得分:2)

情况

What is Double Brace initialization in Java?

new VariableMap<String, String>() {{
    put("Name", "Furze");
}}

创建一个匿名内部类(类VariableMap的子类)并创建其对象。

类和对象都在同一地点创建

all subtypes of a serializable class are themselves serializable以后它仍为Serializable

问题

当您评论时,类定义本身消失

这导致上述ClassNotFoundException

解决方案

  1. 尽可能避免Serialization。实现自己的数据存储机制。
  2. 如果必须使用Serialization,请避免使用双括号初始化。
  3. 在不使用声明的情况下维护声明(test_old方式)。然而,这是一个临时解决方案和不良做法。令人困惑,无法记住。也是未来bug的可能原因。