score:-1
works if simpleclass extends serializable.
score:1
@trenobus @bluenote10 related to your findings:
i just ran into this today and i think it's because the class' name is different (mangled) when you define it in the console. for instance, i serialized a class one
from scala
> import java.io._
import java.io._
> case class one(s: string, b: boolean)
defined class one
> new objectoutputstream(new fileoutputstream("data")) writeobject one("abc", true)
i then tried to deserialize it in java from the same file, having prepared a similar class named one
in the top level (i made serialversionuids be the same as well, didn't include it here since it didn't matter), but got this error:
exception in thread "main" java.lang.classnotfoundexception: $line4.$read$$iw$$iw$one
suggesting that scala (rightfully) creates a new jvm class each time you define a class, and presumably keeps an internal mapping so that you can refer to it by its defined name (one
).
similarly, because each such class is different in jvm, if you redefine class one
in that same scala console, and then try to deserialize from the data
file, you'll get an object of the original class (not of the new class that you redefined).
score:8
this solution works fine for me:
val ois = new objectinputstream(new fileinputstream(filename)) {
override def resolveclass(desc: java.io.objectstreamclass): class[_] = {
try { class.forname(desc.getname, false, getclass.getclassloader) }
catch { case ex: classnotfoundexception => super.resolveclass(desc) }
}
}
of course, when writing object, we use the same way:
val ois = new objectoutputstream(new fileoutputstream(path))
ois.writeobject(myobject)
Source: stackoverflow.com
Related Query
- Scala deserialization: class not found
- Spark Scala getting class not found scala.Any
- Scala IDE Error: Main method not found in class 'hello'
- Scala Main Class not found in Eclipse ide
- Write an Arbitrary Value Not Found in a Case Class Using Play's (2.2) Scala JSON Combinators
- Playframework, scala case class and property not found
- Test Class Not Found when Unit Testing on Scala
- EMR always gives me Class Not Found for Scala app
- Scala class not found on classpath when specifying jar in classpath
- Scala macro expansion of class with companion: type not found
- class not found error - scala
- Error in running Scala Program: Main method not found in class main, please define the main method
- main class not found in spark scala program
- Class not found in Scala template with Play Framework
- Class Not Found running Scala in IntelliJ SBT project
- Error when running Scala project - class scala.deprecatedInheritance in compiler mirror not found
- Java Class not Found Exception while doing Spark-submit Scala using sbt
- Playframework [Scala]: Class not found in classpath in Play for Scala
- Scala class from atmosphere-play not found in Java application
- package scala object is not found when two jars are in class path
- Building Scala Object, Class Not Found
- Scala and Akka : ActorSystem class not found even with the config.jar
- Main class not found error while creating Maven Project in Scala IDE
- Spark Scala Error - Error: Main method not found in class
- Running tests on Intellij: Class not found
- Could not find or load main class in scala in intellij IDE
- IntelliJ Bug: Scala 145, Error:scalac: Scala compiler JARs not found
- Scala Compiler not found in Intellij IDEA 11 with Play 2.0 project
- reduceByKey method not being found in Scala Spark
- After installing Scala using MacPorts, scala command is not found
More Query from same tag
- Making a JVM scala library available in Scala.js
- Transform Value In PlayJSon Mapping
- scala specs2 - how to match a list of matchers or criterias?
- How to write a Scala wrapper for javax.swing.Timer
- Akka Streams filter & group by on a collection of keys
- How to read Avro Binary(Base64) Encoded data in Spark Scala
- Preloading scripts and parsing `args` in the spark-shell
- Scala MongoDb client: from query string to builder
- Import does not bring implicits in scope
- how to convert scala code to java to read xml files
- Scala Type-Inference For Type Constructor
- spray app on Heroku: not able to find script created from stage command
- How to combine two JavaPairRDD's using custom logic
- Apache Spark-Java version 3.3 importing problems
- What is the best way to create and pass around dictionaries containing multiple types in scala?
- Error iterating pairs in sequences
- IntelliJ 11 cannot resolve Play/Scala models without using root package in import statements
- Conversion between time zones results in unexpected behavior
- Way to fetch data periodically in Scala without var/mutable collection
- Kleisli dependencies with Tagless Final style
- Option[Future[Option[Int]]] => Future[Option[Int]]
- Akka Persistence: ReadJournal.runFold never returns
- scala serialization
- Running spark on eclipse in linux
- json4s always escapes unicode character €
- Apple postion in snake game scala
- Flatten list of any in scala
- Spark Scala: Cast StructType to String
- How can I get the binary data from a Class[_] object?
- Play 2.6 indicates to use a deprecated Slick class