Scala MongoDB:使用sbt程序集打包nosuchmethod错误

时间:2017-02-26 21:14:07

标签: mongodb scala sbt nosuchmethoderror sbt-assembly

我已经尝试了一段时间才弄明白为什么在打包后,我得到了  java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V

(根据我所做的,会出现其他错误,但它们都与类路径问题有关)

我首先尝试获取所有Mongo罐子的最新版本 我尝试添加 crossScalaVersions := Seq("2.10.0","2.10.6", "2.12.1")

(在我之前:scalaVersion := "2.12.1"

我尝试将Mongo jar放在lib文件夹中。

我确保sbt clean然后sbt assembly

不确定下一步该尝试什么,我已经在谷歌上搜索了几个小时,并没有找到任何有用的东西。 (我在中间尝试过其他的东西,但我不记得那些东西是x))

我打开不使用程序集,我只是不确定如何在没有它的情况下将这些jar添加到类路径中。

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"
//}

问题的根源(每次private val mongoClient = MongoClient()):

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)
  }

}

就像我说的那样,目前的错误是 java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
或者它也是
java.lang.NoClassDefFoundError: org/mongodb/scala/MongoClient$

我想在旁注时,在取消注释时没有解决插口依赖性问题,但这并不是什么大问题,我只是把它放在lib文件夹中。也适用于plugin.yml - &gt;它实际上并没有进入jar文件,但这很好 - 我只是解压缩,把它放在那里,然后重新拉链。

无论如何,非常感谢异常的任何想法/解决方案,谢谢:)

更新:现在使用gradle,但遗憾的是无济于事:更新

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'
}

我现在得到这个:.NoSuchMethodError:scala.Predef $ .augmentString(Ljava / lang / String;)Ljava / lang / String

1 个答案:

答案 0 :(得分:0)

事实证明我用来加载scig for spigot的scala运行时库已经过时了