启动使用SORM框架的scala项目时出错

时间:2015-09-05 11:17:57

标签: scala intellij-idea playframework sbt sorm

我已经从tutorial创建了简单的sbt项目:

build.sbt:

lazy val sorm_test = (project in file(".")).
settings(
  name := "SORM_TEST",
  scalaVersion := "2.11.7",
  libraryDependencies ++= Seq(
    "org.sorm-framework" % "sorm" % "0.3.18",
    "com.h2database" % "h2" % "1.4.188"
  )
)

test.Main.scala:

package test

case class Artist(
  names : Map[Locale, Seq[String]],
  genres : Set[Genre]
)

case class Genre(
  names : Map[Locale, Seq[String]]
)

case class Locale(
  code : String
)

import sorm._

object Db extends Instance(
  entities = Set(
    Entity[Artist](),
    Entity[Genre](),
    Entity[Locale](unique = Set() + Seq("code"))
  ),
  url = "jdbc:h2:mem:test",
  user = "",
  password = "",
  initMode = InitMode.Create
)

object Main extends App {
  // init
  Db.##
}

当我在intellij中运行这个项目时,我有这样的例外:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1
    at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.parse(ToolBoxFactory.scala:414)
    at sorm.persisted.PersistedClass$.createClass(PersistedClass.scala:107)
    at sorm.persisted.PersistedClass$$anon$1$$anonfun$resolve$1.apply(PersistedClass.scala:125)
    at sorm.persisted.PersistedClass$$anon$1$$anonfun$resolve$1.apply(PersistedClass.scala:125)
    at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:194)
    at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:80)
    at sorm.persisted.PersistedClass$$anon$1.resolve(PersistedClass.scala:125)
    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:497)
    at sorm.persisted.PersistedClass$.apply(PersistedClass.scala:129)
    at sorm.Instance$Initialization$$anonfun$9$$anonfun$apply$16.apply(Instance.scala:239)
    at sorm.Instance$Initialization$$anonfun$9$$anonfun$apply$16.apply(Instance.scala:239)
    at embrace.package$EmbraceAny$.$$extension(package.scala:6)
    at sorm.Instance$Initialization$$anonfun$9.apply(Instance.scala:239)
    at sorm.Instance$Initialization$$anonfun$9.apply(Instance.scala:239)
    at scala.collection.immutable.Set$Set3.foreach(Set.scala:145)
    at sorm.Instance$Initialization.<init>(Instance.scala:239)
    at sorm.Instance.<init>(Instance.scala:38)
    at test.Db$.<init>(Main.scala:15)
    at test.Db$.<clinit>(Main.scala)
    at test.Main$.delayedEndpoint$test$Main$1(Main.scala:29)
    at test.Main$delayedInit$body.apply(Main.scala:27)
    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:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at test.Main$.main(Main.scala:27)
    at test.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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1
    ... 38 more
Caused by: java.lang.ClassNotFoundException: scala.runtime.java8.JFunction1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 38 more

当我使用sbt run时,它没问题。当我将SORM与Play框架集成时,也会抛出此异常。我怎么解决这个问题 ?

2 个答案:

答案 0 :(得分:2)

我通过将dependencyOverrides += "org.scala-lang" % "scala-compiler" % scalaVersion.value添加到我的sbt配置来解决了这个问题。

答案 1 :(得分:-2)

我在运行时期间在maven项目中收到一个非常类似的异常:

    Caused by: java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction0$mcI$sp

我目前正在使用

    <scala.version>2.11.7</scala.version>
    <scala.compat.version>2.11</scala.compat.version>
    <scalatest.version>2.2.2</scalatest.version>
    <scala-maven-plugin.version>3.2.0</scala-maven-plugin.version>
pom属性中的

并在所有其他条目中引用这些属性,例如

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

Maven包装/安装工作正常。这是一个类似的问题吗?有人可以解释上面的解决方案(或者可能将其翻译成maven案例)?