scala序列化总是抛出异常,为什么?

时间:2018-06-10 10:43:36

标签: scala exception serialization

我这个小scala代码snipet:

@SerialVersionUID(43L) class p(val a:Int=14,val b:Double=3.0) extends Serializable{
}
import java.io._

val out = new ObjectOutputStream(new FileOutputStream("my.obj"))
out.writeObject(new p(5,7))
out.close()

val in = new ObjectInputStream(new FileInputStream("my.obj"))
val savedPerson=in.readObject.asInstanceOf[p]
println(savedPerson.a)

当我在Windows或Mac上运行它时,会打印出一个巨大的异常信息:

java.io.NotSerializableException: Main$$anon$1
    at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Main$$anon$1.errorFunction(ch09.scala:52)
    at Main$$anon$1.<init>(ch09.scala:110)
    at Main$.main(ch09.scala:1)
    at Main.main(ch09.scala)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at scala.reflect.internal.util.ScalaClassLoader.$anonfun$run$2(ScalaClassLoader.scala:98)
    at scala.reflect.internal.util.ScalaClassLoader.asContext(ScalaClassLoader.scala:32)
    at scala.reflect.internal.util.ScalaClassLoader.asContext$(ScalaClassLoader.scala:30)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:129)
    at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.scala:98)
    at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.scala:90)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:129)
    at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:22)
    at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:21)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
    at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:29)
    at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:28)
    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
    at scala.tools.nsc.ScriptRunner.runCompiled(ScriptRunner.scala:170)
    at scala.tools.nsc.ScriptRunner.$anonfun$runScript$1(ScriptRunner.scala:187)
    at scala.tools.nsc.ScriptRunner.$anonfun$runScript$1$adapted(ScriptRunner.scala:187)
    at scala.tools.nsc.ScriptRunner.$anonfun$withCompiledScript$2(ScriptRunner.scala:156)
    at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:124)
    at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:200)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:70)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:85)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

这是什么错误?我该如何解决?我不太明白。 谢谢!

1 个答案:

答案 0 :(得分:3)

我从你的堆栈跟踪中看到你正在使用脚本运行器:

scala -nc noser.scala

我在Yessir中命名了脚本,没有先生,因为没有序列化发生。

-nc避免启动编译服务器守护程序。

如果脚本运行器没有看到带有main方法的对象,它会将脚本代码打包到一个类中,该类是它为您构造的main方法的本地方法。

def main(args: Array[String]) = { class anon_class { code } ; new anon_class() }

您的代码在构造函数中运行,这也不是一个好主意。

您可以使用-Xprint:typer,flatten查看此信息。或者试用-Xprint:all

您的可序列化类具有指向匿名本地类的外部指针,并且匿名类不可序列化。

也许外部指针只是一个bug。或者不,请参阅下面的参考。它需要一个提示来消除外部指针,例如创建类final

我发现有类似的错误,例如this one where the enclosing module is captured by a lambda

您的解决方法是将代码放在对象的main方法中,或将所有内容包装在App中。

object Main extends App {
  // code
}

另一种解决方法是制作可序列化的类final。有讨论on this ticketrelated票证和fix