java.lang.NoClassDefFoundError: play/api/libs/json/OWrites$class

1,184 views
Skip to first unread message

Kilic Ali-Firat

unread,
Sep 2, 2016, 2:46:43 AM9/2/16
to ReactiveMongo - http://reactivemongo.org
Hi guys, 

In one simple sbt project (so not play project), I want to use reactivemongo. This is the first time that I use reactivemongo in "no Play scala" project so following the documentation, I created a simple example to see if I can interact with my database.  First, my dependencies in the build.sbt file :

libraryDependencies ++= Seq(
 
"org.reactivemongo" %% "reactivemongo-play-json" % "0.11.14",
 
"org.reactivemongo" % "play2-reactivemongo_2.11" % "0.11.14-play24", // need to add this one because of this follow error missing or invalid dependency detected while loading class file 'GenericCollection.class'
 
"com.bioserenity" % "bioserenity-core_2.11" % "1.0"
)


Then I defined a simple model :

package models


import play.api.libs.json._ // cannot compile without this import
import play.api.libs.functional.syntax._
import scala.concurrent.ExecutionContext.Implicits.global


import reactivemongo.play.json._
import reactivemongo.play.json.collection.{
 
JSONCollection, JsCursor
}, JsCursor._






object BFF_Models {


 
object BlockType extends Enumeration {
    type
BlockType = Value
    val DATA
, GAP = Value


   
implicit val enumFormat = new Format[BlockType] {
     
def reads(json : JsValue) = JsSuccess(BlockType.withName(json.as[String]))
     
def writes(enum : BlockType) = JsString(enum.toString())
   
}
 
}


 
case class BiosChannelBlock
 
(
    blockStartTime
: Long, // timestamp in microseconds
    blockType
: BlockType.Value,
    fileLoc
: Option[String], // the file location
    nsamples
: Int // number of points/samples
 
)


 
object BiosChannelBlock {
   
implicit val BiosChannelBlockFormat = Json.format[BiosChannelBlock]
 
}
}


A simple scala case making a dummy request on the collection :

import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Try, Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.api.MongoConnection
import reactivemongo.play.json._
import reactivemongo.play.json.collection.{
 
JSONCollection, JsCursor
}, JsCursor._


import reactivemongo.api.MongoDriver


object MongoWriter {


 
def findRecordTest() = {
    val uri
= "mongodb://username:password@host:port/"
    val driver
= new MongoDriver
    val connection
: Try[MongoConnection] =
     
MongoConnection.parseURI(uri).map { parsedUri =>
        driver
.connection(parsedUri)
     
}
    connection match
{
     
case Success(c) =>
        val db
= c.db("database")
        val collection
: JSONCollection = db.collection[JSONCollection]("records")
        collection
.find(Json.obj("blockType" -> "BAR")).one[BiosChannelBlock]
     
case Failure(e) =>
        e
.getMessage
   
}


 
}
}



But at the runtime, this is the exception that I get :

[error] (run-main-0) java.lang.NoClassDefFoundError: play/api/libs/json/OWrites$class
java
.lang.NoClassDefFoundError: play/api/libs/json/OWrites$class
 at reactivemongo
.play.json.ImplicitBSONHandlers$JsObjectDocumentWriter$.<init>(package.scala:535)
 at reactivemongo
.play.json.package$.JsObjectDocumentWriter$lzycompute(package.scala:70)
 at reactivemongo
.play.json.package$.JsObjectDocumentWriter(package.scala:70)
 at com
.bioserenity.bff.writer.MongoWriter$.findRecordTest(MongoWriter.scala:32)
 at com
.bioserenity.bff.main.Main$.main(Main.scala:9)
 at com
.bioserenity.bff.main.Main.main(Main.scala)
 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java
.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.ClassNotFoundException: play.api.libs.json.OWrites$class

If I removed the import play.api.libs.json._ in the class with the models, I cannot compile but I don't know if this import hide the reactivemongo.play.json._ import which maybe cause the runtime error. Is it a way to fix this issue ?

Thanks,
Ali-Firat Kilic.

Cédric Chantepie

unread,
Sep 2, 2016, 3:59:30 AM9/2/16
to ReactiveMongo - http://reactivemongo.org
First you have both the ReactiveMongo Play JSON and the Plugin as dependency: if not in a Play app the former is not useful, if in a Play app the first is pulled by the former.

Then you have to provide Playframework JSON lib explicitly: to avoid dependency hell ReactiveMongo Play JSON is tested against Play 2.4.x to 2.5.x, but does impose/pull by itself any dependency about that.

Kilic Ali-Firat

unread,
Sep 2, 2016, 5:10:48 AM9/2/16
to ReactiveMongo - http://reactivemongo.org
Thank you Cédric for the explanations, I can fix my problem and it's more clear for me. 
Reply all
Reply to author
Forward
0 new messages