java - Serializing an anonymous class's function does not work correctly -
summary
when try serialize anonymous function (which includes method) - doesn't seem serialize correctly.
problem
i have interface: program
implements serializable
, contains function void run();
main method creates variable: p
, makes type program
.
to "initialize" this, create anonymous class interface. in there run()
method. type this: system.out.println("hello world, test");
i have class programio
uses objectoutput
, objectinput
streams read , write these files.
writing works, reading - until start reading programio (from: program p = new program {...} program p = programio.readprogram(...)
} when it's - reading , writing doesn't work. (writing shouldn't reading should)
this error: java.lang.classnotfoundexception: main$2
hypotheses
i believe problem occuring because it's looking code inside main class. why want know how can fix this.
code
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 (when writing)
public class main { public static void main(string[] args) { program p = new program() { @override public void run() { system.out.println("hello world, test!"); } }; programio.saveprogram("test.bin", p); } }
main.java (when reading)
public class main { public static void main(string[] args) { program p = programio.readprogram("test.bin"); p.run(); } }
error:
"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 @ java.net.urlclassloader.findclass(urlclassloader.java: 381) @ java.lang.classloader.loadclass(classloader.java: 424) @ sun.misc.launcher$appclassloader.loadclass(launcher.java: 331) @ java.lang.classloader.loadclass(classloader.java: 357) @ java.lang.class.forname0(native method) @ java.lang.class.forname(class.java: 348) @ java.io.objectinputstream.resolveclass(objectinputstream.java: 628) @ java.io.objectinputstream.readnonproxydesc(objectinputstream.java: 1620) @ java.io.objectinputstream.readclassdesc(objectinputstream.java: 1521) @ java.io.objectinputstream.readordinaryobject(objectinputstream.java: 1781) @ java.io.objectinputstream.readobject0(objectinputstream.java: 1353) @ java.io.objectinputstream.readobject(objectinputstream.java: 373) @ programio.readprogram(programio.java: 59) @ main.main(main.java: 13) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java: 62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java: 43) @ java.lang.reflect.method.invoke(method.java: 498) @ com.intellij.rt.execution.application.appmain.main(appmain.java: 144) process finished exit code 0
Comments
Post a Comment