序列化匿名类的功能无法正常工作

时间:2016-05-28 19:33:47

标签: java

摘要

当我尝试序列化我的匿名函数(包括一个方法)时 - 它似乎没有正确地序列化它。

问题

我有一个接口:Program,它实现Serializable并包含函数void run();我的main方法创建一个变量:p并使其为Program类型

To"初始化"这个,我为界面创建了一个匿名类。其中有run()方法。我输入了这个:System.out.println("Hello world, this is a test");

我有另一个类ProgramIO,它使用ObjectOutputObjectInput个流来读取和写入文件。

写作成功,阅读也是如此 - 直到我从ProgramIO开始阅读它(from: Program p = new Program {...} to Program p = ProgramIO.readProgram(...)}当它是这样的时候 - 阅读和写作都不起作用。(写作不应该'但是阅读应该)

这是错误:java.lang.ClassNotFoundException: Main$2

假设

我相信这个问题正在发生,因为它正在寻找主类中的代码。这就是为什么我想知道如何解决这个问题。

代码

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ProgramIO {

public static void saveProgram(String path, Program d) {
    try {
        FileOutputStream e = new FileOutputStream(path);
        ObjectOutputStream os = new ObjectOutputStream(e);
        os.writeObject(d);
        os.close();
        e.close();
    } catch (IOException var4) {
        var4.printStackTrace();
    }

}

public static void autoSaveProgram(String path, Program d) {
    try {
        FileOutputStream fs = new FileOutputStream(path);
        ObjectOutputStream os = new ObjectOutputStream(fs);
        os.writeObject(d);
        os.close();
        fs.close();
    } catch (IOException var4) {

    }

}

public static Program readProgram(String path) {
    Program dbOut = null;

    try {
        FileInputStream e = new FileInputStream(path);
        ObjectInputStream os = new ObjectInputStream(e);
        dbOut = (Program)os.readObject();
        os.close();
        e.close();
    } catch (ClassNotFoundException | IOException var4) {
        var4.printStackTrace();
    }

    return dbOut;
}
}

Program.java

public interface Program {
 public void run();
}

Main.java(写作时)

public class Main {

public static void main(String[] args) {
    Program p = new Program() {
        @Override
        public void run() {
            System.out.println("Hello world, this a test!");
        }
        };

    ProgramIO.saveProgram("test.bin", p);
}
}

Main.java(阅读时)

public class Main {

public static void main(String[] args) {

   Program p = ProgramIO.readProgram("test.bin");
  p.run();
}
}

错误:

"C:\Program Files\Java\jdk1.8.0_91\bin\java" - Didea.launcher.port = 7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\bin" - Dfile.encoding = UTF - 8 - classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\XXXX\Documents\Code\Test Project II\out\production\Test Project II;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\lib\idea_rt.jar"
com.intellij.rt.execution.application.AppMain Main
java.lang.ClassNotFoundException: Main$2
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 java.io.ObjectInputStream.resolveClass(ObjectInputStream.java: 628)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java: 1620)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java: 1521)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java: 1781)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java: 1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java: 373)
at ProgramIO.readProgram(ProgramIO.java: 59)
at Main.main(Main.java: 13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43)
at java.lang.reflect.Method.invoke(Method.java: 498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java: 144)

Process finished with exit code 0

0 个答案:

没有答案