Search code examples
mongodbscalasbtnosuchmethoderrorsbt-assembly

Scala MongoDB: packaging with sbt assembly nosuchmethod error


I've been trying for a while to figure out why after packaging, I get
java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V

(Depending on what I do, other errors appear, but they all have to do with, it seems, class-path problems)

I first tried getting the most up-to-date version for all the Mongo jars.
I tried adding crossScalaVersions := Seq("2.10.0","2.10.6", "2.12.1")

(before I had: scalaVersion := "2.12.1")

I tried putting the Mongo jars in the lib folder.

I make sure to sbt clean and then sbt assembly

Not really sure what to try next, I've been googling around for a few hours and didn't find anything that helped yet. (I've tried other things inbetween but I don't really remember what those things were x) )

I'm open to not using assembly, I just was unsure how to add those jars to the classpath without it.

build.sbt (root dir.):

organization := "com.domain"

name := "ProjectName"

version := "0.1.0-SNAPSHOT"

//scalaVersion := "2.12.1"
crossScalaVersions := Seq("2.10.0","2.10.6", "2.12.1")

//retrieveManaged := true

//resolvers += "spigot-repo" at "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"

libraryDependencies ++= Seq(
    "org.mongodb.scala" % "mongo-scala-driver_2.12" % "1.2.1",
    "org.mongodb.scala" % "mongo-scala-bson_2.12" % "1.2.1",
    "org.mongodb" % "bson" % "3.4.2",
    "org.mongodb" % "mongodb-driver-core" % "3.4.2",
    "org.mongodb" % "mongodb-driver-async" % "3.4.2"
//    "org.spigotmc" % "spigot-api" % "1.11.2-R0.1-SNAPSHOT"
)

//mappings in (Compile, packageBin) <+= baseDirectory map { base =>
//   (base / "in" / "plugin.yml") -> "plugin.yml"
//}

Source of the problem (private val mongoClient = MongoClient() every time):

package com.domain.project.package.name

import org.bson.conversions.Bson
import org.mongodb.scala.MongoClient

class MongoWrapper {

  //  val mongoClientSettings = MongoClientSettings.builder().
  //  val mongoDriverInfo = MongoDriverInformation.builder().
  //  val mongoClient: MongoClient = MongoClients.create(settings, mongoDriverInformation)
  //  private val SERVER = "localhost"
  //  private val PORT = "27017"
  private val DATABASE_NAME = "dbName"

  private val mongoClient = MongoClient()  //#######ERROR HERE########
  val db = mongoClient.getDatabase("dbName")

  var collections: Map[String, CollectionWrapper] = Map()

  def linkCollection(collectionWrapper: CollectionWrapper) {
    collections += collectionWrapper.colName -> collectionWrapper
  }

  def queueRead(caller: Any, colIn: String, matcher: Bson, func: Any) {
    val colOut = collections.get(colIn).get
  }

  def queueWrite(caller: Any, colIn: String, data: Bson, func: Any) {    
    val colOut = collections.get(colIn).get
    colOut.write(caller, data, func)
  }

}

Like I said, the current error is
java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
or it's also been
java.lang.NoClassDefFoundError: org/mongodb/scala/MongoClient$

I suppose on a side-note the spigot dependency is unresolved when uncommented, but that's not a big deal, I just put it in the lib folder. Also for the plugin.yml -> it doesn't actually make it to the jar file, but that's fine - I'm just unzipping, putting it in there, and rezipping.

Anyways any ideas/solutions to the Exception(s) would be greatly appreciated, thanks :)

UPDATE: using gradle now, but to no avail unfortunately :UPDATE

apply plugin: 'java'
apply plugin: 'scala'

repositories {
    jcenter()
    mavenCentral()
}

jar {
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

dependencies {

    compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.1'

    compile files('lib/spigot-api-1.11.2-R0.1-SNAPSHOT-shaded.jar')

    compile('org.mongodb.scala:mongo-scala-driver_2.11:1.2.1') {
        exclude group: 'org.scala-lang'
    }
}

buildscript {
    dependencies {
        classpath fileTree(dir: 'lib', include: '*.jar')
    } 
}

 jar {
    from "plugin.yml"

    baseName = 'ProjectName'
    version = '0.1.0-SNAPSHOT'
}

I get this now: .NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Ljava/lang/String


Solution

  • It turns out the scala runtime library I was using to load scala for spigot was out of date