使用SBT创建的Uber / Fat Jar上的NoClassDefFoundError

时间:2017-05-19 16:38:09

标签: scala log4j sbt sbt-assembly

我正在使用sbt assembly插件来创建Uber/Fat jar。但获得以下Exception

scala -cp ./target/scala-2.11/test.jar com.LoggingTest
java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)

我已将log4j作为我的依赖项的一部分(请参阅下面的build.sbt文件)。不确定为什么我仍然得到NoClassDefFoundError

lazy val root = (project in file(".")).
  settings(
    name := "myproject",
    version := "1.0",
    scalaVersion := "2.11.8",
    libraryDependencies ++= Seq(
        "org.apache.logging.log4j" % "log4j-api" % "2.8",
        "org.apache.logging.log4j" % "log4j-core" % "2.8",
        "org.apache.logging.log4j" %% "log4j-api-scala" % "2.8"
    )
 )

// Adding scala style to compilation process
lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value
(compile in Compile) <<= (compile in Compile) dependsOn compileScalastyle


assemblyMergeStrategy in assembly := {
  case PathList("javax", "servlet", xs @ _*) => MergeStrategy.last
  case PathList("javax", "activation", xs @ _*) => MergeStrategy.last
  case PathList("javax", "el", xs @ _*) => MergeStrategy.last
  case PathList("org", "apache", xs @ _*) => MergeStrategy.last
  case PathList("com", "google", xs @ _*) => MergeStrategy.last
  case PathList("com", "esotericsoftware", xs @ _*) => MergeStrategy.last
  case PathList("com", "codahale", xs @ _*) => MergeStrategy.last
  case PathList("com", "yammer", xs @ _*) => MergeStrategy.last
  case PathList("org", "jboss", xs @ _*) => MergeStrategy.last
  case PathList("io", "netty", xs @ _*) => MergeStrategy.discard
  case "about.html" => MergeStrategy.rename
  case "META-INF/ECLIPSEF.RSA" => MergeStrategy.last
  case "META-INF/mailcap" => MergeStrategy.last
  case "META-INF/mimetypes.default" => MergeStrategy.last
  case "META-INF/jboss-beans.xml" => MergeStrategy.last
  case "plugin.properties" => MergeStrategy.last
  case "log4j.properties" => MergeStrategy.last
  case "project.properties" => MergeStrategy.last
  case "META-INF/io.netty.versions.properties" => MergeStrategy.last
  case x =>
      val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)
}    

以下是插件定义文件$PROJECT_HOME/project/plugins.sbt

的完整内容
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")

我也是unzipped jar,发现Jar中没有类(org.apache.log4j.logger.Logger)。我希望Class可以在Jar中使用,因为log4j已被添加为sbt依赖项的一部分。我确实找到了课程org.apache. 记录 .log4j.Logger

PS:我也正在使用jar为某些类执行spark-submit(因为我正在使用spark api进行一些计算)并且它没有任何问题

0 个答案:

没有答案