Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassNotFoundException during readObject() #89

Closed
ihji opened this issue Jul 6, 2011 · 6 comments
Closed

ClassNotFoundException during readObject() #89

ihji opened this issue Jul 6, 2011 · 6 comments
Labels
Milestone

Comments

@ihji
Copy link
Contributor

ihji commented Jul 6, 2011

The following simple example works fine with scalac and scala execution but throws ClassNotFoundException in sbt run. The experimental setup is sbt 0.10 and scala 2.9.0-1.

Sample scala file:

case class A(name: String, list: List[Type])

sealed abstract class Type
case object ObjType extends Type

case class SomeMap(t: Map[A,Int]) {
  def get(a: A) : Option[Int] = { 
    t.get(a)
  }
}

object Test extends App {
  import java.io._
  val o = new ObjectOutputStream(new FileOutputStream("tmp.bin"))
  val map = SomeMap(Map(A("asdf",List(ObjType))->1))
  o.writeObject(map)
  o.close

  val i = new ObjectInputStream(new FileInputStream("tmp.bin"))
  val a = i.readObject.asInstanceOf[SomeMap]
  println("should be Some(1): "+a.get(A("asdf",List(ObjType))))
}

sbt run log:


java.lang.ClassNotFoundException: ObjType$
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:603)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1574)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    at scala.collection.immutable.$colon$colon.readObject(List.scala:404)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    at Test$delayedInit$body.apply(A.scala:20)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:60)
    at scala.App$$anonfun$main$1.apply(App.scala:60)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:45)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
    at scala.App$class.main(App.scala:60)
    at Test$.main(A.scala:12)
    at Test.main(A.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sbt.Run.invokeMain(Run.scala:60)
    at sbt.Run.run0(Run.scala:53)
    at sbt.Run.execute$1(Run.scala:42)
    at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:46)
    at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
@harrah
Copy link
Member

harrah commented Jul 6, 2011

Not obviously a bug. Please use the mailing list for questions.

@harrah harrah closed this as completed Jul 6, 2011
@ihji
Copy link
Contributor Author

ihji commented Jul 7, 2011

I found that adding fork in run := true option resolves the problem. Mentioning this kind of serialization problem in https://github.com/harrah/xsbt/wiki/Running-Project-Code would be good. Anyway, thanks for your help.

@Blaisorblade
Copy link
Contributor

@ihji, I'm adding a mention of this to the Wiki, as suggested. Please take a look and feel free to improve.

@retronym
Copy link
Member

retronym commented Mar 19, 2015

Here's a related report on the Scala side: https://issues.scala-lang.org/browse/SI-9237 scala/bug#9237

@eed3si9n
Copy link
Member

eed3si9n commented Oct 6, 2018

Ref scala/bug#9237
Ref scala/scala-dev#562 (comment)

According to scala/bug#9237 (comment), Java serialization ObjectInputStream#resolveClass specifically is incompatible with Serialization Proxy pattern and sbt's layered ClassLoader, which previously contained the issue most around List. The situation has changed in 2018 when Scala 2.13 adopted Serialization Proxy pattern for all collections. Also more prevalent use of Java serialization thanks to Spark.

I am reopening this issue.

@eed3si9n
Copy link
Member

We will be able to work around this issue once sbt 1.3.0 comes out using #4476.
Here's how the build looks like:

ThisBuild / scalaVersion     := "2.12.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "sbt-89",
    Compile / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat,
  )

run works:

sbt:sbt-89> run
should be Some(1): Some(1)t

[success] Total time: 1 s, completed Mar 19, 2019 12:10:47 PM

eed3si9n pushed a commit to eed3si9n/sbt that referenced this issue Sep 22, 2020
Use v0.4.16 of dagre-d3 to avoid javascript error
eed3si9n pushed a commit to eed3si9n/sbt that referenced this issue Apr 20, 2021
0.13.9 + use bintray-sbt to release to Bintray
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants