2

I'm trying to make work a very simple example of Salat.

build.sbt:

libraryDependencies += "com.novus" %% "salat" % "1.9.9"

In sbt console:

import com.novus.salat._
import com.novus.salat.global._
import com.mongodb.casbah.Imports._

case class Alpha(x: String)
val a = Alpha(x = "Hello world")
val dbo = grater[Alpha].asDBObject(a)  // not working

The last line throws an exception:

 GRATER GLITCH - unable to find or instantiate a grater using supplied path name

  REASON: Very strange!  Path='Alpha' from pickled ScalaSig causes ClassNotFoundException

  Context: 'global'
  Path from pickled Scala sig: 'Alpha'

I can't figure out what's wrong, after 2 hours of looking at examples on the internet I couldn't find one that I could make work. Are they all outdated or am I wrong somewhere in my example?

thomas legrand
  • 493
  • 1
  • 5
  • 16

1 Answers1

1

SBT interfere Salat's way of loading class somehow and it's unable to find your class with its default classloader. Test the code not in sbt console, but create a simple project and run it.

UPDATE: as Thomas pointed out Salat needs case class to be compiled and be on classpath to be able to load it.

build.sbt:

libraryDependencies += "com.novus" %% "salat" % "1.9.9"

HelloWorld.scala:

import com.novus.salat._
import com.novus.salat.global._

case class Alpha(x: String)

object HelloWorld {
  def main(args: Array[String]): Unit = {
    val a = Alpha(x = "Hello world")
    val dbo = grater[Alpha].asDBObject(a)
    println(dbo.toString)
  }
}
  • No, it doesn't work. I don't see your point of doing that either. – thomas legrand Jan 14 '16 at 08:02
  • I used salat with Play and to make it work for me I had to replace class loader salat uses by default with Play's class loader. I'll update the answer now. – Yury Sukhoverkhov Jan 14 '16 at 09:49
  • I'm not talking about making it work with Play. I just want to make it work in standalone. – thomas legrand Jan 14 '16 at 09:50
  • So you do not want me to show you how I resolve the same issue you have when I'm using play and try to make something similar with standalone? As you wish :). – Yury Sukhoverkhov Jan 14 '16 at 09:52
  • Since I'm not using Play, I'd like to be able to make it work without any Play dependency, yes. But looks like everybody uses it with Play, since there is nothing on stackoverflow about my problem... – thomas legrand Jan 14 '16 at 09:56
  • The problem - salat's default classloader is unable to load the class. I do not know why exactly it happens to you. Play's classloader is not a solution for your problem. Using Salat with Play creates the problem similar to yours by logs (exactly the same actually) and using Play's classloader (while using Salat with Play) fixes it. So if you use Salat standalone default Salat's classloader should work fine and adding dependency to Play to use Play's classloader is not a solution. Could you please make a small SBT project what reproduces the problem and post it somewhere? – Yury Sukhoverkhov Jan 14 '16 at 10:08
  • My example is already reproducible. Just create the build.sbt file as I indicated, then do "sbt console" and paste the code i entered. – thomas legrand Jan 14 '16 at 10:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100665/discussion-between-yury-sukhoverkhov-and-thomas-legrand). – Yury Sukhoverkhov Jan 14 '16 at 11:59
  • I've just made a small project with your code and it perfectly works. And at the same time it does not work in sbt console. SBT interfere Salat's way of loading class somehow and it's unable to find your class with his default classloader. Just follow my advice, make a project and run it! Do not test this conde in SBT console. I updated the answer with the exact code I used. – Yury Sukhoverkhov Jan 14 '16 at 14:03
  • 1
    Thanks a lot Yury, so this library needs the case class to be already compiled into Alpha.class to work... – thomas legrand Jan 15 '16 at 14:13
  • Yes, this the reason of the error, thank you for pointing out to that. I'll update the answer with your information. – Yury Sukhoverkhov Jan 15 '16 at 20:32