在Scala中引用序列化类的正确方法

时间:2014-06-09 05:45:30

标签: java scala serialization

我正在尝试引用已在Scala中序列化的另一个类,而且我是Java的新手。这是我遇到问题的代码示例

import scala.io.Source
import java.io._

class testcl extends Serializable {

  val teststr = "serializestring"

}


class revivetest {

    val file = "file.serial"

    val fis = new FileInputStream(file)
    val ois = new ObjectInputStream(fis)

 val revived = ois.readObject.asInstanceOf[testcl]

 }

以下是通过REPL加载类时的行为:

  

阶> val test = new revivetest

java.lang.ClassNotFoundException:testcl     在scala.tools.nsc.interpreter.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:83)     at java.lang.ClassLoader.loadClass(ClassLoader.java:425)     at java.lang.ClassLoader.loadClass(ClassLoader.java:358)     at java.lang.Class.forName0(Native Method)     在java.lang.Class.forName(Class.java:270)     at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:625)     at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)     at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)     at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)     at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)     在revivetest。(:26)     at。(:20)     在 。()     7岁时)     在 。()     at $ print()     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     在scala.tools.nsc.interpreter.IMain $ ReadEvalPrint.call(IMain.scala:734)     在scala.tools.nsc.interpreter.IMain $ Request.loadAndRun(IMain.scala:983)     在scala.tools.nsc.interpreter.IMain.loadAndRunReq $ 1(IMain.scala:573)     在scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)     在scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)     在scala.tools.nsc.interpreter.ILoop.reallyInterpret $ 1(ILoop.scala:760)     在scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:805)     在scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:717)     在scala.tools.nsc.interpreter.ILoop.processLine $ 1(ILoop.scala:581)     在scala.tools.nsc.interpreter.ILoop.innerLoop $ 1(ILoop.scala:588)     在scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:591)     在scala.tools.nsc.interpreter.ILoop $$ anonfun $ process $ 1.apply $ mcZ $ sp(ILoop.scala:882)     在scala.tools.nsc.interpreter.ILoop $$ anonfun $ process $ 1.apply(ILoop.scala:837)     在scala.tools.nsc.interpreter.ILoop $$ anonfun $ process $ 1.apply(ILoop.scala:837)     在scala.tools.nsc.util.ScalaClassLoader $ .savingContextLoader(ScalaClassLoader.scala:135)     在scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:837)     在scala.tools.nsc.MainGenericRunner.runTarget $ 1(MainGenericRunner.scala:83)     在scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)     在scala.tools.nsc.MainGenericRunner $ .main(MainGenericRunner.scala:105)     在scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

  

问题:引用另一个已序列化的类以避免java.lang.ClassNotFoundException的正确方法是什么?

0 个答案:

没有答案